PDA

View Full Version : First and Last Bit of GetAsyncKeyState



fishchoke
12-04-2011, 03:15 PM
I'm trying to add code to my macro which will interupt the macro if the user presses any key during macro execution. I have had some luck with the GetAsyncKeyState function. I managed to get it to tell me which keys are pressed right at the moment that I call the function, but I can't get it to tell me what keys were pressed recently / since the last time I called the function.

I'm reading this:
msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx

which says:
If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.

Not being a super experience programmer, I do not know how to get the most significant bit and the least significant bit. I read up on what those are and I get it, but I just can't find anything that can tell me HOW to convert the getasynckeystate return into an 8 or 10 digit binary value that I can read individual bits from. I had a feeling that if I had a degree in computer science, this would be elementary, but, since I don't I have to hope you guys will take pity on me.

Can anyone point me in the right direction?

My main goal here is to determine which key was pressed, and if that key is down now, or if it's up.

Here is the code I'm messing with now:

Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer

Public Function IsAnyKeyPressed() As Boolean
If KeyPressedNumber = 0 Then
IsAnyKeyPressed = False
Else
IsAnyKeyPressed = True
End If
End Function
Private Sub TestIsAnyKeyPressed()
'(it is best if you start this macro via a button on a spreasheet, not by using F5 from VBE editor)
AppActivate "notepad"
For i = 1 To 8888
Cells(1, 1).Value = i
Cells(1, 2).Value = KeyPressedNumber
Cells(1, 3).Value = IsAnyKeyPressed
If Cells(1, 2) = 27 Then End
If InStr(1, Cells(1, 2).Value, "27", vbTextCompare) Then End
DoEvents
Next i
End Sub


Public Function KeyPressedNumber() As Integer
For i = 65 To 90
Cells(1, 4).Value = (GetAsyncKeyState(i) And &H8001)
If (GetAsyncKeyState(i) And &H8001) <> 0 Then
Cells(1, 5).Value = (GetAsyncKeyState(i) And &H8001)
KeyPressedNumber = i
Exit Function
End If
Next i
KeyPressedNumber = 0
End Function
Private Sub TestKeyPressedNumber()
'(it is best if you start this macro via a button on a spreasheet, not by using F5 from VBE editor)
AppActivate "notepad"
For i = 1 To 8888
Cells(1, 1).Value = i
Cells(1, 2).Value = KeyPressedNumber
If Cells(1, 2).Value = 27 Then End
If InStr(1, Cells(1, 2).Value, "27", vbTextCompare) Then End
DoEvents
Next i
End Sub


Public Function KeysPressedNumbers() As Variant
KeysPressedNumbers = ""
For i = 0 To 255
If (GetAsyncKeyState(i) And &H8001) <> 0 Then
If KeysPressedNumbers = "" Then
KeysPressedNumbers = i
Else
KeysPressedNumbers = KeysPressedNumbers & ", " & i
End If
End If
Next i
End Function
Private Sub TestKeysPressedNumbers()
'(it is best if you start this macro via a button on a spreasheet, not by using F5 from VBE editor)
AppActivate "notepad"
For i = 1 To 8888
Cells(1, 1).Value = i
Cells(1, 2).Value = KeysPressedNumbers
If Cells(1, 2).Value = 27 Then End
If InStr(1, Cells(1, 2).Value, "27", vbTextCompare) Then End
DoEvents
Next i
End Sub

Kenneth Hobs
12-04-2011, 03:53 PM
Look at the code in this tetris game. http://excelunusual.com/archive/2010/12/a-game-of-tetris-in-excel/

fishchoke
12-04-2011, 05:32 PM
Thanks very much but I am not seeing any answer there.

I can see George is using getasynckeystate, but that's about it.
I can't see any way he is determining the most and least significant bits, or determining if the arrow keys are pressed now, or were pressed recently.
What am I missing?