Sign in to follow this  
Followers 0
JonnyThunder

How to read keypress

5 posts in this topic

Ello,

I'm writing something to allow programmable actions for keys on a laptop. How can I return the keycode from a key being pressed, without knowing what the key is beforehand??

Thanks,

JT

Share this post


Link to post
Share on other sites



Actually, just found this script elsewhere on the forum - but doesn't solve my problem....

Global Const $WH_KEYBOARD_LL = 13
Global $hHook
Global $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
Global $hmod = DllCall("kernel32.dll", "hwnd", "GetModuleHandle", "ptr", 0)
Global $hHook = DllCall("user32.dll", "hwnd", "SetWindowsHookEx", "int", _
        $WH_KEYBOARD_LL, "ptr", DllCallbackGetPtr($hStub_KeyProc), "hwnd", $hmod[0], "dword", 0)
Global $buffer = ""

While 1
    Sleep(10)
WEnd

Func EvaluateKey($keycode)
    Msgbox(0, 'Key Pressed', '$keyCode = ' & $keyCode & @CRLF & 'Chr($keyCode) = ' & Chr($keyCode))
    
    If $keyCode = 65 then 
        Msgbox(0, 'Winner!', 'Somebody pressed A')
    EndIf
EndFunc  ;==>EvaluateKey

Func _KeyProc($nCode, $wParam, $lParam)
    Local $ret, $KEYHOOKSTRUCT
    If $nCode < 0 Then
        $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hHook[0], _
                "int", $nCode, "wparam", $wParam, "lparam", $lParam)
        Return $ret[0]
    EndIf
    If $wParam = 256 Then
        $KEYHOOKSTRUCT = DllStructCreate("dword;dword;dword;dword;ptr", $lParam)
        EvaluateKey(DllStructGetData($KEYHOOKSTRUCT, 1))
    EndIf
    $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hHook[0], _
            "int", $nCode, "ptr", $wParam, "ptr", $lParam)
    Return $ret[0]
EndFunc  ;==>_KeyProc

Func OnAutoItExit()
    DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $hHook[0])
    DllCallbackFree($hStub_KeyProc)
EndFunc  ;==>OnAutoItExit

When running this, it doesn't pick up the media keys on laptops (anything outside of the actual standard keyboard layout). Does anyone know of a way to do this??

Share this post


Link to post
Share on other sites

This is just a shot in the dark, but study in the AutoIt help file under:

User Defined Functions > Misc Management > _IsPressed

And under:

AutoIt > Appendix > Send Key List > (see bottom of page for Media buttons)

There may be an undocumented code you could send to _IsPressed


Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

Hi, run this code and check the scite console while pressing keys to find the vkey code for the key you pressed. On my laptop my Fn key doesn't register, but my volume/mute/wifi/info/screenswap buttons all show for me.

#include <Misc.au3>

While Not _IsPressed("1B")
    Sleep(100)
    For $i = 1 To 255
        If _IsPressed(StringFormat("%02x", $i)) Then ConsoleWrite(StringFormat("%02x", $i) & @LF)
    Next
Wend

Cheers

Share this post


Link to post
Share on other sites

Thanks guys, i'll give that a shot and see what buttons it'll recognise.

I guess some buttons aren't recognized because a driver is required to identify them under Windows.

Share this post


Link to post
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
Sign in to follow this  
Followers 0