Trapping Key presses in VB using GetAsyncKeyState 
Author Message
 Trapping Key presses in VB using GetAsyncKeyState

I am trying to record a subset of virtual key presses, such as Enter and
Tab, using the Windows API GetAsyncKeyState function. According to the API
documentation this returns a value containing the current key state in the
most significant bit and in the least significant bit (LSB)  whether the key
was pressed since the last time the GetAsyncKeyState function was called (I
presume for that key but it is not 100% clear).

I start the process using the following code (where gintVirtKeys is a list
of VKMAX length containing all the virtual keys I want to check)...

    ' Clear the current keyboard pressed states
    '
    For intChar = 1 To VKMAX
        GetAsyncKeyState gintVirtKeys(intChar)
    Next

    ' Start the timer process
    SetTimer hwnd, 0, 2, AddressOf TimerProc

The timer then records and futher keypress'...

    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse
As Long, ByVal lpTimerFunc As Long)

        Dim intChar As Integer
        Dim intVal As Integer

        For intChar = 1 To VKMAX

             ' Get the new key state
             '
             intVal = GetAsyncKeyState(gintVirtKeys(intChar))

             ' Check against the LSB to see if the key was pressed since we
last called the function
             '
             If (intVal And 1) <> 0 Then

                 ' Record keypress here

             End If

        Next

    End Sub

Unfortunately, the function always returns 0 even though I know a key has
been pressed. I tried removing the "clear" loop in the initialization code
which causes the correct value to be returned but it also returns values for
any keys that have been pressed previously at any time during the thread's
life.

Has anyone any experience using this API function with VB and give me advice
as to what I need to do to get this to work? Should I be using one of the
other keyboard state functions instead (GetKeyboardState, GetKeyState)?

Any help appreciated
Cheers
Rich



Sun, 23 Feb 2003 18:25:32 GMT  
 Trapping Key presses in VB using GetAsyncKeyState



?I am trying to record a subset of virtual key presses, such as Enter and
?Tab, using the Windows API GetAsyncKeyState function.

(snip)

Hi Rich,

Seems to work for me (NT4-SP5, VB6-SP3), at least for ENTER and TAB.  Are you
using these values for the virtual key constants?

  Const VK_RETURN = &HD
  Const VK_TAB = &H9



Sun, 23 Feb 2003 21:30:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to figure out if any key has been pressed (GetAsyncKeyState)

2. trapping function key press

3. Trapping Funtion Key Presses

4. Trapping Key Press Events

5. Trap cursor/arrow key presses

6. How to trap the TAB key press.

7. Trapping two or more keys pressed at the same time

8. Trap key presses in all other apps too?

9. Trap keys pressed to ole container with word document embedded in it on win 95/98

10. How to trap pressed key in TreeView LabelEdit?

11. Trap key press in Console

12. Key Down and Key press

 

 
Powered by phpBB® Forum Software