Sign in to follow this  
Followers 0
sandin

detect Alt UP via Key.Proc.

6 posts in this topic

Hi,

The following example has been taken from help files, and modded a bit, it can detect left Alt down and up (as well as left Ctrl, Shift, Enter...), but it can't detect right Alt UP (as well as Ctrl, Shift, Numeric Enter....), and my question is: Why? And is there a way to make it detect right controls UP like it's doing with the left ones?

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
Global $hHook, $hStub_KeyProc, $buffer = ""
_Main()
Func _Main()
    Local $hmod
    $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hmod = _WinAPI_GetModuleHandle(0)
    $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>_Main
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    Local $flags = DllStructGetData($tKEYHOOKS, "flags")
    Switch $flags
        Case $LLKHF_ALTDOWN
            ConsoleWrite("$LLKHF_ALTDOWN" & @LF)
        Case $LLKHF_UP
            ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @LF)
    EndSwitch
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc
Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>OnAutoItExit

Note that my main concern is right ALT, I don't really care about right Ctrl, Shift... (Case $LLKHF_EXTENDED can detect right Ctrl or Shift DOWN but it can't detect right ALT Down), but I noticed that those keys aren't working either so I thought they might be connected somehow, that's why I mentioned them.

Share this post


Link to post
Share on other sites



Hehe :P

Here is answer:

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>
Global $hHook, $hStub_KeyProc, $buffer = ""
_Main()
Func _Main()
    Local $hmod
    $hStub_KeyProc = DllCallbackRegister("_KeyProc", "long", "int;wparam;lparam")
    $hmod = _WinAPI_GetModuleHandle(0)
    $hHook = _WinAPI_SetWindowsHookEx($WH_KEYBOARD_LL, DllCallbackGetPtr($hStub_KeyProc), $hmod)
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>_Main
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    Local $flags = DllStructGetData($tKEYHOOKS, "flags")
    Switch $flags
        Case $LLKHF_ALTDOWN
            ConsoleWrite("$LLKHF_ALTDOWN" & @LF)
        Case $LLKHF_UP
            ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @LF)
        Case 33
            ConsoleWrite("RIGHT ALT DOWN" & @LF)
        Case 129
            ConsoleWrite("RIGHT ALT UP" & @LF)
    EndSwitch
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc
Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>OnAutoItExit

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

Hmm... I see... But you can do this (it's little ugly, but hey, it works :P)

Global $raltdown = False
...

        Case 33
            $raltdown = True
            ConsoleWrite("RIGHT ALT DOWN " & $flags & @LF)
        Case 129
            If $raltdown Then
                $raltdown = False
                ConsoleWrite("RIGHT ALT UP " & $flags & @LF)
                MsgBox(0,"Hmm...", "Ugly but it works :D")
            EndIf
...

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

I see...

Here is my finally try :P

Just add this in your script from first post (add after "Case $LLKHF_UP" - line 27):

...
Case 129
            If DllStructGetData($tKEYHOOKS, "scanCode") = 56 And DllStructGetData($tKEYHOOKS, "vkCode") = 165 Then
            ConsoleWrite("RIGHT ALT UP" & @LF)
            EndIf
...

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

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