I'm a noob to NumGet / NumPut and DllCall, but somehow I managed to get most of this working
Problem is, I can't figure out how to properly get information from the "stroke" structure nor how to properly put data into it. But I did manage to easily change the key code that intercept sends.
To get this working you need to download interception.zip from here: https://github.com/oblitum/Interception/releases/
Extract it to a folder
go to the \commad line installer\ directory
"Run install-interception without any arguments inside an console executed as administrator and it will give instructions for installation."
This installs the driver.
Next, copy "interception.dll" from either \library\x32\ or \library\x64\ into the same directory as the script below
Also make sure that you use a unicode build of AHK, ANSI just doesn't work.
This demo script will transform your "Control" key into "X".
Pressing a key or using the mouse wheel will show a tooltip with info about it.
Pressing Escape will exit the while loop and the script.
For fun, try using:
Code: Select all
get_hardware_id(context,device)
Then use it in an if statement to send a different key or do something else.
Have a peek at set_filter()'s static vars for a list of strings you can use.
For the keyboard, use the ones beginning with "FILTER_", not "KEY_"
For the mouse, everything seems to work, but I have no clue how to change movement.
Code: Select all
;#include <_Struct>
#SingleInstance force
load()
context:=create_context()
set_filter(context, "keyboard","FILTER_KEY_ALL|FILTER_KEY_E0|FILTER_KEY_E1")
set_filter(context, "mouse", "MOUSE_WHEEL")
while ( recieve(context,device:=wait(context),stroke,1) >0 )
{
if is_keyboard(device)
{
code:=hex(NumGet(stroke,0,"Ushort"))
if (code=0x01) ;escape EXIT
break
s:=get_stroke(device,stroke)
if (s.name="LCONTROL")
{
key:=key2dec("X")
NumPut(key,stroke,0,"Ushort")
}
send(context,device,&stroke,1)
}
else if is_mouse(device)
{
;tooltip mouse!
s:=get_stroke(device,stroke)
send(context,device,&stroke,1)
}
}
return
get_stroke(device,stroke)
{
if is_keyboard(device)
{
/*
interception.h:
typedef struct
{
unsigned short code;
unsigned short state;
unsigned int information;
} InterceptionKeyStroke;
msdn:
typedef struct _KEYBOARD_INPUT_DATA {
USHORT UnitId;
USHORT MakeCode;
USHORT Flags;
USHORT Reserved;
ULONG ExtraInformation;
} KEYBOARD_INPUT_DATA, *PKEYBOARD_INPUT_DATA;
*/
o:={}
o.code := hex(NumGet(stroke,0,"Ushort"))
o.name:=GetKeyName("sc" o.code)
o.state:= NumGet(stroke,2,"Short")
o.info:= NumGet(stroke,4,"Uint")
ToolTip % "(Press Escape to Exit)`n`nkeyboard:`nname=" o.name "`ncode=" o.code "`nstate=" o.state "`ninfo=" o.info
}
else
{
/*
interception.h:
typedef struct
{
unsigned short state;
unsigned short flags;
short rolling;
int x;
int y;
unsigned int information;
} InterceptionMouseStroke;
msdn:
typedef struct _MOUSE_INPUT_DATA {
USHORT UnitId;
USHORT Flags;
_ANONYMOUS_UNION union {
ULONG Buttons;
_ANONYMOUS_STRUCT struct {
USHORT ButtonFlags;
USHORT ButtonData;
} DUMMYSTRUCTNAME;
} DUMMYUNIONNAME;
ULONG RawButtons;
LONG LastX;
LONG LastY;
ULONG ExtraInformation;
} MOUSE_INPUT_DATA, *PMOUSE_INPUT_DATA;
*/
o:={}
size:=2
o.state:= NumGet(stroke,0,"ushort")
o.flags:= NumGet(stroke,2,"ushort")
o.rolling:= NumGet(stroke,4,"short")
o.x:= NumGet(stroke,6,"int")
o.y:= NumGet(stroke,10,"int")
o.info:= NumGet(stroke,14,"uint")
ToolTip % "(Press Escape to Exit)`n`nmouse:`nstate=" o.state "`nflag=" o.flags "`nrolling=" o.rolling "`nx=" o.x "`ny=" o.y "`ninfo=" o.info
}
return o
}
send(context,device,stroke,nstroke){
global dll
;interception_send(InterceptionContext context, InterceptionDevice device, const InterceptionStroke *stroke, unsigned int nstroke);
res:=DllCall(dll . "\interception_send","Ptr",context,"int",device,"ptr",stroke,"int",nstroke,"Cdecl Int")
return res
}
recieve(context,device,ByRef stroke,ByRef nstroke){
global dll
;interception_receive(InterceptionContext context, InterceptionDevice device, InterceptionStroke *stroke, unsigned int nstroke);
VarSetCapacity(stroke,500)
res:=DllCall(dll . "\interception_receive","Ptr",context,"int",device,"str",stroke,"int*",nstroke,"Cdecl Int")
return res
}
get_hardware_id(context,device){
global dll
VarSetCapacity(buffer,500)
if (a_ptrsize > 4)
DllCall(dll . "\interception_get_hardware_id","Ptr",context,"int",device,"str",buffer,"Uint",500,"Cdecl Int")
else if (a_ptrsize < 4)
DllCall(dll . "\interception_get_hardware_id","Ptr",context,"int",device,"ptr",&buffer,"Uint",500,"Cdecl Int")
return a_ptrsize > 4 ? buffer : StrGet(&buffer, "UTF-16")
}
wait(context){
global
return DllCall(dll . "\interception_wait","Ptr",context,"Cdecl Int")
}
set_filter(context,filter,keys){
global dll
Static keyboard:="interception_is_keyboard"
,mouse:="interception_is_mouse"
,KEY_DOWN = 0x00
,KEY_UP = 0x01
,KEY_NONE = 0x0000
,KEY_ALL = 0xFFFF
,KEY_E0 = 0x02 ;Delete Key
,KEY_E1 = 0x04
,KEY_TERMSRV_SET_LED = 0x08
,KEY_TERMSRV_SHADOW = 0x10
,KEY_TERMSRV_VKPACKET = 0x20
,FILTER_KEY_NONE = 0x0000
,FILTER_KEY_ALL = 0xFFFF
,FILTER_KEY_DOWN := KEY_UP
,FILTER_KEY_UP := KEY_UP << 1
,FILTER_KEY_E0 := KEY_E0 << 1
,FILTER_KEY_E1 := KEY_E1 << 1
,FILTER_KEY_TERMSRV_SET_LED := KEY_TERMSRV_SET_LED << 1
,FILTER_KEY_TERMSRV_SHADOW := KEY_TERMSRV_SHADOW << 1
,FILTER_KEY_TERMSRV_VKPACKET := KEY_TERMSRV_VKPACKET << 1
,MOUSE_NONE = 0x0000
,MOUSE_ALL = 0xFFFF
,MOUSE_LEFT_BUTTON_DOWN = 0x001
,MOUSE_LEFT_BUTTON_UP = 0x002
,MOUSE_RIGHT_BUTTON_DOWN = 0x004
,MOUSE_RIGHT_BUTTON_UP = 0x008
,MOUSE_MIDDLE_BUTTON_DOWN= 0x010
,MOUSE_MIDDLE_BUTTON_UP = 0x020
,MOUSE_BUTTON_1_DOWN := MOUSE_LEFT_BUTTON_DOWN
,MOUSE_BUTTON_1_UP := MOUSE_LEFT_BUTTON_UP
,MOUSE_BUTTON_2_DOWN := MOUSE_RIGHT_BUTTON_DOWN
,MOUSE_BUTTON_2_UP := MOUSE_RIGHT_BUTTON_UP
,MOUSE_BUTTON_3_DOWN := MOUSE_MIDDLE_BUTTON_DOWN
,MOUSE_BUTTON_3_UP := MOUSE_MIDDLE_BUTTON_UP
,MOUSE_BUTTON_4_DOWN = 0x040
,MOUSE_BUTTON_4_UP = 0x080
,MOUSE_BUTTON_5_DOWN = 0x100
,MOUSE_BUTTON_5_UP = 0x200
,MOUSE_WHEEL = 0x400
,MOUSE_HWHEEL = 0x800
,MOUSE_MOVE = 0x1000
,MOUSE_MOVE_RELATIVE = 0x000
,MOUSE_MOVE_ABSOLUTE = 0x001
,MOUSE_VIRTUAL_DESKTOP = 0x002
,MOUSE_ATTRIBUTES_CHANGED = 0x004
,MOUSE_MOVE_NOCOALESCE = 0x008
,MOUSE_TERMSRV_SRC_SHADOW = 0x100
if Instr(keys,"|")
{
loop,parse,keys,|
{
key:=%a_loopfield%
if a_index=1
keys2:=key
else
keys2:=keys2|key
}
keys:=keys2
}
else
keys:=%keys%
f:=get_pointer(%filter%)
DllCall(dll . "\interception_set_filter","ptr",context,"ptr",f, "int",keys,"Cdecl Int")
}
is_mouse(device){
global
return DllCall(dll . "\interception_is_mouse","int",device)
}
is_keyboard(device){
global
return DllCall(dll . "\interception_is_keyboard","int",device)
}
get_pointer(func){
global
return DllCall("GetProcAddress", ptr, dllh, "astr", func, ptr)
}
create_context(){
global
return DllCall(dll . "\interception_create_context")
}
load(){
global
dll:="interception.dll"
dllh:=DllCall("LoadLibrary", "Str", dll, "Ptr")
}
hex(n){
SetFormat, IntegerFast, hex
n+= 0 ; Sets Var (which previously contained 11) to be 0xb.
n.= "" ; Necessary due to the "fast" mode.
SetFormat, IntegerFast, d
return n
}
dec(n){
SetFormat, Integer, D
Dec := (n) + 0
return Dec
}
key2dec(name){
hex:=GetKeySC(name)
return dec(hex)
}