Jump to content

_WinAPI_CallNextHookEx Example doesnt detect lower-case?


Recommended Posts

I looked at the example in the autoit help file for _WinAPI_CallNextHookEx, as it works perfect for an overlay I'm creating. If you run that example, it runs a keyboard hook. In the comments, it shows its supposed to show a different $keycode for lower-case and upper-case letters, but I only get uppercase keycodes. Does anyone else get this too? Is there anyway I can modify the example to work like its supposed to, to show a different $keycode for lower-case letters?

Example: Type a and A, it will show in the Debug Traytip only A, instead of 'a' then 'A'.

In the comments, it shows it returns different keycodes for 'a' and 'A'

;Overlay Keyhook Part
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>

Opt('MustDeclareVars', 1)

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)

    MsgBox(4096, "", "Click OK, then in notepad type..." & _
            @LF & @LF & "Jon" & @LF & "AutoIt" & @LF & @LF & "Press Esc to exit script")

    Run("Notepad")
    WinWait("Untitled -")
    WinActivate("Untitled -")

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>_Main

Func EvaluateKey($keycode)
    If (($keycode > 64) And ($keycode < 91)) _ ; a - z
            Or (($keycode > 96) And ($keycode < 123)) _ ; A - Z
            Or (($keycode > 47) And ($keycode < 58)) Then ; 0 - 9
            TrayTip($keycode,Chr($keycode),5)
        $buffer &= Chr($keycode)
        Switch $buffer
            Case "Jon"
                ToolTip("What can you say?")
            Case "AutoIt"
                ToolTip("AutoIt Rocks")
        EndSwitch
    ElseIf ($keycode > 159) And ($keycode < 164) Then
        Return
    ElseIf ($keycode = 27) Then ; esc key
        Exit
    Else
        $buffer = ""
    EndIf
EndFunc   ;==>EvaluateKey

;===========================================================
; callback function
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN Then
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
    Else
        Local $flags = DllStructGetData($tKEYHOOKS, "flags")
        Switch $flags
            Case $LLKHF_ALTDOWN
                ConsoleWrite("$LLKHF_ALTDOWN" & @LF)
            Case $LLKHF_EXTENDED
                ConsoleWrite("$LLKHF_EXTENDED" & @LF)
            Case $LLKHF_INJECTED
                ConsoleWrite("$LLKHF_INJECTED" & @LF)
            Case $LLKHF_UP
                ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @LF)
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>OnAutoItExit
Edited by CrewXp
Link to comment
Share on other sites

I looked at the example in the autoit help file for _WinAPI_CallNextHookEx, as it works perfect for an overlay I'm creating. If you run that example, it runs a keyboard hook. In the comments, it shows its supposed to show a different $keycode for lower-case and upper-case letters, but I only get uppercase keycodes. Does anyone else get this too? Is there anyway I can modify the example to work like its supposed to, to show a different $keycode for lower-case letters?

Example: Type a and A, it will show in the Debug Traytip only A, instead of 'a' then 'A'.

;Overlay Keyhook Part
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <StructureConstants.au3>

Opt('MustDeclareVars', 1)

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)

    MsgBox(4096, "", "Click OK, then in notepad type..." & _
            @LF & @LF & "Jon" & @LF & "AutoIt" & @LF & @LF & "Press Esc to exit script")

    Run("Notepad")
    WinWait("Untitled -")
    WinActivate("Untitled -")

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>_Main

Func EvaluateKey($keycode)
    If (($keycode > 64) And ($keycode < 91)) _ ; a - z
            Or (($keycode > 96) And ($keycode < 123)) _ ; A - Z
            Or (($keycode > 47) And ($keycode < 58)) Then ; 0 - 9
            TrayTip($keycode,Chr($keycode),5)
        $buffer &= Chr($keycode)
        Switch $buffer
            Case "Jon"
                ToolTip("What can you say?")
            Case "AutoIt"
                ToolTip("AutoIt Rocks")
        EndSwitch
    ElseIf ($keycode > 159) And ($keycode < 164) Then
        Return
    ElseIf ($keycode = 27) Then ; esc key
        Exit
    Else
        $buffer = ""
    EndIf
EndFunc   ;==>EvaluateKey

;===========================================================
; callback function
;===========================================================
Func _KeyProc($nCode, $wParam, $lParam)
    Local $tKEYHOOKS
    $tKEYHOOKS = DllStructCreate($tagKBDLLHOOKSTRUCT, $lParam)
    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
    EndIf
    If $wParam = $WM_KEYDOWN Then
        EvaluateKey(DllStructGetData($tKEYHOOKS, "vkCode"))
    Else
        Local $flags = DllStructGetData($tKEYHOOKS, "flags")
        Switch $flags
            Case $LLKHF_ALTDOWN
                ConsoleWrite("$LLKHF_ALTDOWN" & @LF)
            Case $LLKHF_EXTENDED
                ConsoleWrite("$LLKHF_EXTENDED" & @LF)
            Case $LLKHF_INJECTED
                ConsoleWrite("$LLKHF_INJECTED" & @LF)
            Case $LLKHF_UP
                ConsoleWrite("$LLKHF_UP: scanCode - " & DllStructGetData($tKEYHOOKS, "scanCode") & @TAB & "vkCode - " & DllStructGetData($tKEYHOOKS, "vkCode") & @LF)
        EndSwitch
    EndIf
    Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>_KeyProc

Func OnAutoItExit()
    _WinAPI_UnhookWindowsHookEx($hHook)
    DllCallbackFree($hStub_KeyProc)
EndFunc   ;==>OnAutoItExit
I think you need to use something like GetKeyboardState to see if CAPS Lock is on or the shift key is pressed.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Okay. That was my next approach, coding shift checks into the program. But I didn't want to do this, as I'm trying to stay away from the '_IsPressed way'.

That and.... the example kind of made me believe it should have detected it.

It says...

If (($keycode > 64) And ($keycode < 91)) _; a - z
            Or (($keycode > 96) And ($keycode < 123)) _; A - Z
            Or (($keycode > 47) And ($keycode < 58)) Then; 0 - 9
            TrayTip($keycode,Chr($keycode),5)

If you look at the commented out part to the right, There's different keycodes for both a-z and also A-Z. I thought the hook differentiated between case switches. Weird.

Link to comment
Share on other sites

*bump* If anyone knows if its just my computer that isn't returning the 64-91 $keycodes for lower-case, or if an update to autoit changed it, please let me know, so I can research and debug this to find out why.

Otherwise, Ill consider the Example incorrect :D And will use another approach. Damn, wanted to learn more about low-level hooking.

Link to comment
Share on other sites

There is an ASCII difference, not virtual key difference. Windows programs use TranslateMessage() API function to post the correct character to the window procedure, so as martin said, it's required to check if the caps lock is toggled and what is the state of the shift when a character is pressed. It's quite the same check that TranslateMessage() will do to determine what character to post in the wParam of WM_CHAR for example.

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