Jump to content

How to Check If All Modifier Keys are Up?


Zohar
 Share

Recommended Posts

Hi all


I have this function that I use for years, that checks if all Modifier Keys are up:

Func    KeyBoard_WaitForModifierKeysUp()
    While(_IsPressed("10") Or _IsPressed("11") Or _IsPressed("12") Or _IsPressed("5B") Or _IsPressed("5C"))     ;Shift,Ctrl,Alt,LWin,RWin
        Sleep(50)
    WEnd
EndFunc


It uses the _IsPressed() function, and calls it 5 times on each run.

While it works very well,
I am curious If it's possible to achieve the same functionality, via another function, with only a single call.

For example, a function that returns a bit-set, with 1 it for every modifier key,
and then you test the bits..

I assume there isn't a built-in AutoIt function that does it,
so maybe there's a Windows API function that can do it?


Thank you

Link to comment
Share on other sites

a possible way

#include <WinAPISys.au3>

; _WinAPI_GetKeyboardState fails if there is not a GUI
$hGUI = GUICreate("Test _WinAPI_GetKeyboardState")
GUISetState()

KeyBoard_WaitForModifierKeysUp() ; returns when Shift,Ctrl,Alt,LWin,RWin ar all UP
MsgBox(0, '', "All modifier Keys are Up")

Func KeyBoard_WaitForModifierKeysUp()
    Local $aKeyboardState
    Do
        $aKeyboardState = StringRegExp(StringTrimLeft(DllStructGetData(_WinAPI_GetKeyboardState(), 1), 2), "\w{2}", 3)
        ConsoleWrite("Debug: " & $aKeyboardState[16] & ' ' & $aKeyboardState[17] & ' ' & $aKeyboardState[18] & ' ' & $aKeyboardState[91] & ' ' & $aKeyboardState[92] & @LF)
        Sleep(50)
    Until (Not (BitAND(0x80, BitOR( _
            "0x" & $aKeyboardState[16], _     ; Shift
            "0x" & $aKeyboardState[17], _     ; Ctrl
            "0x" & $aKeyboardState[18], _     ; Alt
            "0x" & $aKeyboardState[91], _     ; LWin
            "0x" & $aKeyboardState[92]))))    ; RWin

EndFunc   ;==>KeyBoard_WaitForModifierKeysUp

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

6 hours ago, Zohar said:

Thank you very much Chimp.

You are welcome

6 hours ago, Zohar said:

Can you please explain the RegExp line? What does "\w{2}" do?

... have a look to this other topic https://www.autoitscript.com/forum/topic/206464-regex-to-prepend-0x-to-string-chunks

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...