Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - VBA

Страница: 1 |

 

  Вопрос: SetTimer + GetAsyncKeyState + VBA Excel Добавлено: 04.09.09 10:18  

Автор вопроса:  AngryBadger
Уважаемые, как сделать чтобы это сцу#о отлавливало нажатия кнопок в Do-Loop'е все ОК, все ловит, а с таймером не хочет почему-то, а при использовании [/source]If GetAsyncKeyState(VK_LEFT) Then
Debug.Print "Left" [/source] при одном нажатии печатает несколько раз. Вообщем как их соединить, чтобы при разовом нажатии отлавливалось одно нажатие? Я вот думал как-нибудь hook использовать, но рук и мозга пока нехватает.


Собственно код, который не работает:
[source]
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Declare Function Getasynckeystate Lib "user32" Alias "GetAsyncKeyState" (ByVal VKEY As Long) As Integer
Public elpTm As Long
Public tmrID As Long
Const VK_LEFT As Long = &H25
Const VK_RIGHT As Long = &H27
Const VK_DOWN As Long = &H28
Const VK_UP As Long = &H26
Const VK_SPACE As Long = &H20

Sub main()
 tmrID = SetTimer(&H0, &H0, 1, AddressOf tmrPrc)
End Sub

Sub tmrKill()
 KillTimer &H0, tmrID
End Sub


Public Sub tmrPrc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTimer As Long)
elpTm = elpTm + 1
    
    keystate = Getasynckeystate(37)
        If (keystate And &H1) = &H1 Then Debug.Print "Left"
    keystate = Getasynckeystate(39)
        If (keystate And &H1) = &H1 Then Debug.Print "Right"
    keystate = Getasynckeystate(38)
        If (keystate And &H1) = &H1 Then Debug.Print "Down"
    keystate = Getasynckeystate(40)
        If (keystate And &H1) = &H1 Then Debug.Print "Up"
    keystate = Getasynckeystate(32)
        If (keystate And &H1) = &H1 Then Debug.Print "Space"

If elpTm = 1000 Then
tmrKill
elpTm = 0
End If

End Sub
[/source]

Ответить

  Ответы Всего ответов: 1  

Номер ответа: 1
Автор ответа:
 AngryBadger



Вопросов: 33
Ответов: 245
 Профиль | | #1 Добавлено: 04.09.09 10:20
  1. Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
  2. Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
  3. Public Declare Function Getasynckeystate Lib "user32" Alias "GetAsyncKeyState" (ByVal VKEY As Long) As Integer
  4. Public elpTm As Long
  5. Public tmrID As Long
  6. Const VK_LEFT As Long = &H25
  7. Const VK_RIGHT As Long = &H27
  8. Const VK_DOWN As Long = &H28
  9. Const VK_UP As Long = &H26
  10. Const VK_SPACE As Long = &H20
  11.  
  12. Sub main()
  13.  tmrID = SetTimer(&H0, &H0, 1, AddressOf tmrPrc)
  14. End Sub
  15.  
  16. Sub tmrKill()
  17.  KillTimer &H0, tmrID
  18. End Sub
  19.  
  20.  
  21. Public Sub tmrPrc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTimer As Long)
  22. elpTm = elpTm + 1
  23.     
  24.     keystate = Getasynckeystate(37)
  25.         If (keystate And &H1) = &H1 Then Debug.Print "Left"
  26.     keystate = Getasynckeystate(39)
  27.         If (keystate And &H1) = &H1 Then Debug.Print "Right"
  28.     keystate = Getasynckeystate(38)
  29.         If (keystate And &H1) = &H1 Then Debug.Print "Down"
  30.     keystate = Getasynckeystate(40)
  31.         If (keystate And &H1) = &H1 Then Debug.Print "Up"
  32.     keystate = Getasynckeystate(32)
  33.         If (keystate And &H1) = &H1 Then Debug.Print "Space"
  34.  
  35. If elpTm = 1000 Then
  36. tmrKill
  37. elpTm = 0
  38. End If
  39.  
  40. End Sub

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам