Hey everyone.
Got another good question I could use some help with.
I just started using Micah's HID support DLL, and I'm trying to do some tricks to get the "most" use out of my media keys. Now that I'm using my keyboard in USB again, since the HID routines themselves are now handling the "extended" keys of my keyboard, which don't send the normal scancodes, I've decided to simulate them instead. A neat way I thought to do it.
What I'm getting, however, isn't what I hoped for..
Here's a code sample I threw out just to test things with:
Code:
;
; AutoHotkey Version: 1.x
; Language: English
; Platform: WinXP
; Author: Eric Renfro <erenfro@gmail.com>
;
; Script Function:
; Test Snippet for Logitech Coordless Freedom HID support.
;
Init()
{
HID_DLL=%A_ScriptDir%\AutohotkeyRemoteControl.dll
hModule := DllCall("LoadLibrary", "str", HID_DLL)
OnMessage(0x00FF, "HID_MSG")
DetectHiddenWindows, On
HWND := WinExist(A_ScriptFullPath . " ahk_class AutoHotkey")
HookHID(1, 12, HWND)
SetTimer, UPDATEDSCRIPT, 1000
OnExit, cleanup
Return
}
Init()
vkF8sc03B & y::
MsgBox, 32, Messenger Test, Load Yahoo Messenger,1
Exit
vkF8sc03B & m::
MsgBox, 32, Messenger Test, Load MSN Messenger,1
Exit
vkF8sc03B::
MsgBox, 32, Messenger Test, Load Trillian,1
Exit
Return
HandleHID(Vals)
{
;MsgBox, 32, HandleHID, Vals %Vals%, 1
ifequal, Vals, 485756
kb_Do("Messenger", "Down")
ifequal, Vals, 005756
kb_Do("Messenger", "Up")
ifequal, Vals, 515352
kb_Do("Webcam", "Down")
ifequal, Vals, 005352
kb_Do("Webcam", "Up")
}
ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4)
; pSource is a string (buffer) whose memory area contains a raw/binary integer at pOffset.
; The caller should pass true for pSigned to interpret the result as signed vs. unsigned.
; pSize is the size of PSource's integer in bytes (e.g. 4 bytes for a DWORD or Int).
; pSource must be ByRef to avoid corruption during the formal-to-actual copying process
; (since pSource might contain valid data beyond its first binary zero).
{
SourceAddress := &pSource + pOffset ; Get address and apply the caller's offset.
result := 0 ; Init prior to accumulation in the loop.
Loop %pSize% ; For each byte in the integer:
{
result := result | (*SourceAddress << 8 * (A_Index - 1)) ; Build the integer from its bytes.
SourceAddress += 1 ; Move on to the next byte.
}
if (!pIsSigned OR pSize > 4 OR result < 0x80000000)
return result ; Signed vs. unsigned doesn't matter in these cases.
; Otherwise, convert the value (now known to be 32-bit) to its signed counterpart:
return -(0xFFFFFFFF - result + 1)
}
Return
HookHID(Usage, Page, HWND)
{
nRC := DllCall("AutohotkeyRemoteControl\RegisterDevice", INT, Usage, INT, Page, INT, HWND, "Cdecl UInt")
if (errorlevel <> 0) || (nRC == -1)
{
MsgBox RegisterDevice fehlgeschlagen. Errorcode: %errorlevel%
ExitApp
;goto cleanup
}
}
Return
HID_MSG(wParam, lParam, msg, hwnd)
{
local Vals
DataSize = 5000
VarSetCapacity(RawData, %DataSize%)
nRC := DllCall("AutohotkeyRemoteControl\GetWM_INPUTHIDData", UINT, wParam, UINT, lParam, "UINT *" , DataSize, "UINT *", RawData, "Cdecl UInt")
if (errorlevel <> 0) || (nRC == -1)
{
MsgBox GetWM_INPUTHIDData fehlgeschlagen. Errorcode: %errorlevel%
ExitApp
;goto cleanup
}
loop, %DataSize%
{
Zeichen := ExtractInteger(RawData, A_Index, false, 1)
ifless, Zeichen, 9
{
Zeichen= 0%Zeichen%
}
Vals = %Vals%%Zeichen%
}
HandleHID(Vals)
Vals = ""
}
Return
;reload the script if you changed it with your editor
UPDATEDSCRIPT:
FileGetAttrib,attribs,%A_ScriptFullPath%
IfInString,attribs,A
{
FileSetAttrib,-A,%A_ScriptFullPath%
SplashTextOn,,,Updated script,
Sleep,500
Reload
}
Return
cleanup:
DllCall("FreeLibrary", "UInt", hModule) ; It is best to unload the DLL after using it (or before the script exits).
ExitApp
Return
kb_Do(Key, Direction)
{
;MsgBox, 32, kbDo, Got to kbDo with %Key% and %Direction%, 1
if Key = Messenger
{
If Direction = Down
Send {vkF8sc03B DOWN}
If Direction = Up
Send {vkF8sc03B UP}
;Gosub HotKeyChecks
}
}
Return
Now, the problem, here-in lies with this batch of code:
Code:
vkF8sc03B & y::
MsgBox, 32, Messenger Test, Load Yahoo Messenger,1
Exit
vkF8sc03B & m::
MsgBox, 32, Messenger Test, Load MSN Messenger,1
Exit
vkF8sc03B::
MsgBox, 32, Messenger Test, Load Trillian,1
Exit
With all of them there, autohotkey seems to do absolutely nothing with it. Keep in just the last one, and a popup comes up with Load Trillian. Anyone got any ideas about why this is, or how to fix it?
--
Psi-Jack
PS: Micha, hope you like how I reorganized and functionized your example scripts.

I'm not a big fan of the old Gosub or Goto type flows.