AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

29 Jun 2018, 13:06

Tweaked some stuff to try and debug what is going on with PS/2 devices.
I altered the GetDeviceList() function to return all devices, even those without a VID and PID
This should make *something* appear in the monitor script for PS/2 devices
Furthermore I added a "Handle" parameter, so we can see the raw device handles
Attached a zip with the changed files
Attachments
PS2.zip
(11.26 KiB) Downloaded 268 times
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

30 Jun 2018, 16:15

@malcev I see no issue with subscribing to up and down, but I do see an issue with SendKeyEvent and ScanCodes > 256

Test script (Should remap Up to Down and vice versa):

Code: Select all

#SingleInstance force
#Persistent
#include Lib\AutoHotInterception.ahk

AHI := new AutoHotInterception()

;~ keyboardId := AHI.GetKeyboardId(0x04F2, 0x0112)
keyboardId := AHI.GetKeyboardId(0x413C, 0x2107)
AHI.SubscribeKey(keyboardId, GetKeySC("Up"), true, Func("KeyEvent").Bind(GetKeySC("Down")))
AHI.SubscribeKey(keyboardId, GetKeySC("Down"), true, Func("KeyEvent").Bind(GetKeySC("Up")))
return

KeyEvent(code, state){
	global AHI, keyboardId
	AHI.SendKeyEvent(keyboardId, code, state)
	ToolTip % "State: " state
}

^Esc::
	ExitApp

The way Interception represents right variants of keys (eg Right ALT etc) means that if SC is > 256, I do some custom logic.
This seems to not work for arrow keys

Attached is a replacement DLL which should let you send arrow keys, but I am not sure how to deal with this going forwards.
I have raised an Issue on GitHub regarding this
Attachments
AutoHotInterception.zip
(8.51 KiB) Downloaded 162 times
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 08:13

Image
How can I use keyboardId := AHI.GetKeyboardId(0x413C, 0x2107) with handle only?
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 12:22

malcev wrote: How can I use keyboardId := AHI.GetKeyboardId(0x413C, 0x2107) with handle only?
You can't at the moment, which is what I intend to rectify.
For the time being, you can just pass the interception ID instead.
eg

Code: Select all

keyboardId := 1
AHI.SubscribeKey(keyboardId, GetKeySC("Up"), true, Func("KeyEvent").Bind(GetKeySC("Down")))
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 12:37

Attached is a test of a new version that should let you get device ID from handle
Added methods:
GetKeyboardIdFromHandle(handle, instance := 1)
GetMouseIDFromHandle(handle, instance := 1)

Please test it all works for you, and I will push out a new release
I am guessing AHI.GetKeyboardIdFromHandle("ACPI\PNP0303") should work for you
Attachments
AutoHotInterception.zip
(375.32 KiB) Downloaded 142 times
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 12:52

I think here is error:

Code: Select all

	GetKeyboardIdFromHandle(handle, instance := 1){
		return this.GetDeviceId(false, handle, instance)
	}
	
	GetMouseIDFromHandle(handle, instance := 1){
		return this.GetDeviceId(true, handle, instance)
	}
It should be:

Code: Select all

	GetKeyboardIdFromHandle(handle, instance := 1){
		return this.GetDeviceIdFromHandle(false, handle, instance)
	}
	
	GetMouseIDFromHandle(handle, instance := 1){
		return this.GetDeviceIdFromHandle(true, handle, instance)
	}
And if I use this code

Code: Select all

#SingleInstance force
#Persistent
#include Lib\AutoHotInterception.ahk
AHI := new AutoHotInterception()
keyboardId := 1
AHI.SubscribeKey(keyboardId, GetKeySC("Up"), true, Func("KeyEvent").Bind(GetKeySC("Down")))
AHI.SubscribeKey(keyboardId, GetKeySC("Down"), true, Func("KeyEvent").Bind(GetKeySC("Up")))
return

KeyEvent(code, state){
	global AHI, keyboardId
	AHI.SendKeyEvent(keyboardId, code, state)
	ToolTip % "State: " state
}
then "up" remaps to "P" and down remaps to "H".
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 13:02

Good catch on the first one

Regarding the up/down not mapping again - that did not make it into this commit. The version I posted for you before was a bodge just to make sure I identified what the issue was.
I will be looking into a proper fix for that one ASAP
Attached is a DLL with these new changes, and the Up/Down bodge
Attachments
AutoHotInterception.zip
(8.61 KiB) Downloaded 136 times
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 13:31

AHA! I took another look at the code that deals with ScanCode > 255, and it was broken for all of them, not just up.

Old code:

Code: Select all

        public void SendKeyEvent(int id, ushort code, int state)
        {
            var stroke = new ManagedWrapper.Stroke();
            if (code > 255)
            {
                code -= 256;
                state += 2;
            }
            stroke.key.code = code;
            stroke.key.state = (ushort)(1 - state);
            ManagedWrapper.Send(_deviceContext, id, ref stroke, 1);
        }
DUH!! Interception uses 0 for press, 1 for release, or +2 to each of those if extended.
The above logic clearly only works properly when ScanCodes are < 256
It should do 1-state, and THEN add 2 if needed

New code:

Code: Select all

        public void SendKeyEvent(int id, ushort code, int state)
        {
            IsValidDeviceId(false, id);
            var st = 1 - state;
            var stroke = new ManagedWrapper.Stroke();
            if (code > 255)
            {
                code -= 256;
                st += 2;
            }
            stroke.key.code = code;
            stroke.key.state = (ushort)st;
            ManagedWrapper.Send(_deviceContext, id, ref stroke, 1);
        }
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

02 Jul 2018, 14:06

AHI 0.3.5 has been released!

Adds support for Horizontal Wheel on Mice - thanks @crumbl3d !
Instance is now correctly passed through in GetDeviceID and variants
GetDeviceIdFromHandle, GetKeyboardIdFromHandle, GetMouseIDFromHandleadded to enable support for PS/2 and Laptop devices
Fix for SendKeyEvent when using ScanCodes > 255
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

06 Jul 2018, 17:48

Code: Select all

MonitorWrapper := new AutoHotInterception("Monitor")
Monitor := MonitorWrapper.Instance
Monitor.Subscribe(Func("KeyboardEvent"), Func("MouseEvent"))
And then how to unload an object MonitorWrapper, and unsubscribe?
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

07 Jul 2018, 04:50

evilC wrote:Adds support for Horizontal Wheel on Mice

Code: Select all

AHI.Instance.SendMouseButtonEvent(12, 6, 1)
I tried it but nothing happens, should the mouse physically support it?
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

07 Jul 2018, 06:59

I now understand it is necessary to use to unsubscribe

Code: Select all

Monitor.SetDeviceFilterState(dev.id, 0)
But is it necessary to destroy the Monitor and MonitorWrapper objects?
And where to read about SetDeviceFilterState and the rest?
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

07 Jul 2018, 10:48

Fix for SendKeyEvent when using ScanCodes > 255
It is impossible to send Numlock - sc145.
Although it sends sc145 but at the same time vkFF is not vk90.
But if to send sc45 then it is defined as Numlock, although sc45 is Pause.
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

09 Jul 2018, 07:46

evilC wrote:The Monitor was never designed to be loaded / unloaded dynamically
SubscribeKey too it is impossible to unloaded ?
serzh82saratov
Posts: 137
Joined: 01 Jul 2017, 03:04

Re: AutoHotInterception (AHI): Multi-Keyboard / Multi-Mouse support for AHK. Per-device blocking!

09 Jul 2018, 08:07

It seems to me that in such a topic it is necessary to unload all methods, including the instance itself.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 130 guests