Jump to content

Key Chording example using _WinAPI_GetKeyboardState


AutoXenon
 Share

Recommended Posts

#include <WinAPIvkeysConstants.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Init()
Do
Until GUIGetMsg()=-3

Func Init()
     Global $hWndKeybd = GUICreate('')
     Local $tRID = DllStructCreate($tagRAWINPUTDEVICE)
     DllStructSetData($tRID, 'UsagePage', 0x01) ; Generic Desktop Controls
     DllStructSetData($tRID, 'Usage', 0x06) ; Keyboard
     DllStructSetData($tRID, 'Flags', $RIDEV_INPUTSINK)
     DllStructSetData($tRID, 'hTarget', $hWndKeybd)
     _WinAPI_RegisterRawInputDevices($tRID)
     GUIRegisterMsg($WM_INPUT, WM_INPUT)
     GUISetState(@SW_SHOW)
EndFunc

Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam)
        #forceref $iMsg, $wParam
     Switch $hWnd
       Case $hWndKeybd
            Local $tRIK = DllStructCreate($tagRAWINPUTKEYBOARD)
            If _WinAPI_GetRawInputData($lParam, $tRIK, DllStructGetSize($tRIK), $RID_INPUT) Then KeystrokeLogic($tRIK)
     EndSwitch
     If $wParam Then Return 0
EndFunc

Func KeystrokeLogic($tRIK)
     If $VK_SPACE = DllStructGetData($tRIK,'VKey') And BitAnd(1, DllStructGetData($tRIK,'Flags')) Then ; on spacebar release
        Local $tKeys = _WinAPI_GetKeyboardState()
        Local $chord = ( BitAnd(128,DllStructGetData($tkeys,1,1+$VK_Q)) ? 1 : 0 ) _
                     + ( BitAnd(128,DllStructGetData($tkeys,1,1+$VK_W)) ? 2 : 0 ) _
                     + ( BitAnd(128,DllStructGetData($tkeys,1,1+$VK_E)) ? 4 : 0 ) ; add one due to DllStructGetData being 1-based while the byte array is 0-based
        Switch $chord
          Case 1
               Q()
          Case 2
               W()
          Case 3
               QW()
          Case 4
               E()
          Case 5
               EQ()
          Case 6
               WE()
          Case 7
               QWE()
        EndSwitch
     EndIf
EndFunc

Func Q()
     Beep(384,200)
EndFunc
Func W()
     Beep(432,200)
EndFunc
Func E()
     Beep(512,200)
EndFunc
Func QW()
     Beep(480,200)
EndFunc
Func WE()
     Beep(640,200)
EndFunc
Func EQ()
     Beep(576,200)
EndFunc
Func QWE()
     Beep(720,200)
EndFunc

Originally posted here, thought perhaps it merits its own topic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...