Visual Basic, .NET, ASP, VBA, VBScript
 
  Библиотека кодов  
  Мышь и клавиатура  
     
  Отслеживание нажатий на клавишы клавиатуры  
  Чем мне понравился этот пример? Тем, что без использования таймеров ваша программа может реагировать на нажатия клавиш клавиатуры. Причем время реакции отклика на нажатия, как мне показалось, гораздо выше.

Вам понадобится элемент CommandButton и элемент PictureBox.
Private m_bPlay As Boolean
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
Dim i As Long
Dim iLast As Long
If Command1.Caption = "&Stop" Then
m_bPlay = False
Command1.Caption = "&Play"
Else
Command1.Caption = "&Stop"
m_bPlay = True
i = 1
Do
' Determine if the left or right keys are pressed:
If (GetAsyncKeyState(vbKeyLeft)) Then
' Diminish the colour
i = i - 1
ElseIf (GetAsyncKeyState(vbKeyRight)) Then
' Increase the colour
i = i + 1
End If
' Colour within bounds:
If (i < 1) Then i = 15
If (i > 15) Then i = 1
' If colour has changed, change the display:
If (iLast <> i) Then
With Picture1
.Cls
.ForeColor = QBColor(i)
' Generate a RGB complement for the background:
.BackColor = &HFFFFFF And (Not QBColor(i))
.CurrentX = 64 * Screen.TwipsPerPixelX
.CurrentY = 64 * Screen.TwipsPerPixelY
Picture1.Print Hex$(QBColor(i))
End With
End If
iLast = i
' This is here to stop the animation getting too fast to see:
Sleep 25
' Ensure we can still click buttons etc
DoEvents
Loop While m_bPlay
End If
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If (Command1.Caption = "&Stop") Then
Command1_Click
End If
End Sub
 
     
  VBNet online (всего: 52050)  
 

Логин:

Пароль:

Регистрация, забыли пароль?


В чате сейчас человек
 
     
  VBNet рекомендует  
   
     
  Лучшие материалы  
 
ActiveX контролы (112)
Hitman74_Library (36119)
WindowsXPControls (20739)
FlexGridPlus (19374)
DSMAniGifControl (18295)
FreeButton (15157)
Примеры кода (546)
Parol (18027)
Passworder (9299)
Screen saver (7654)
Kerish AI (5817)
Folder_L (5768)
Статьи по VB (136)
Мое второе впечатление... (11236)
VB .NET: дорога в будущее (11161)
Основы SQL (9225)
Сообщения Windows в Vi... (8788)
Классовая теория прогр... (8619)
 
     
Техническая поддержка MTW-хостинг | © Copyright 2002-2011 VBNet.RU | Пишите нам