ContextManager, Wrap all methods

FullWrapper
evilC 6 years ago
parent 2717e494ee
commit 9ef97fb18b

@ -9,11 +9,15 @@ namespace AutoHotInterception.Helpers
{
public static class HelperFunctions
{
public static bool IsValidDeviceId(bool isMouse, int device)
public static void IsValidDeviceId(bool isMouse, int id)
{
var start = isMouse ? 11 : 1;
var end = start + 9;
return device >= start && device <= end;
if (id < start || id > end)
{
throw new ArgumentOutOfRangeException(nameof(id), $"Invalid id ID: {id} for device type {(isMouse ? "Mouse" : "Keyboard")}. Device IDs for this type should be between {start} and {end}");
}
//return id >= start && id <= end;
}
public static void GetVidPid(string str, ref int vid, ref int pid)

@ -57,13 +57,10 @@ namespace AutoHotInterception
/// <param name="block">Whether or not to block the key</param>
/// <param name="callback">The callback to fire when the key changes state</param>
/// <returns></returns>
public bool SubscribeKey(int id, ushort code, bool block, dynamic callback)
public void SubscribeKey(int id, ushort code, bool block, dynamic callback)
{
SetFilterState(false);
if (!IsValidDeviceId(false, id))
{
return false;
}
IsValidDeviceId(false, id);
if (!_keyboardMappings.ContainsKey(id))
{
@ -75,7 +72,6 @@ namespace AutoHotInterception
SetFilterState(true);
SetThreadState(true);
return true;
}
/// <summary>
@ -86,12 +82,9 @@ namespace AutoHotInterception
/// <param name="block">Whether or not to block the button</param>
/// <param name="callback">The callback to fire when the button changes state</param>
/// <returns></returns>
public bool SubscribeMouseButton(int id, ushort btn, bool block, dynamic callback)
public void SubscribeMouseButton(int id, ushort btn, bool block, dynamic callback)
{
if (!IsValidDeviceId(true, id))
{
return false;
}
IsValidDeviceId(true, id);
if (!_mouseButtonMappings.ContainsKey(id))
{
@ -102,7 +95,6 @@ namespace AutoHotInterception
SetFilterState(true);
SetThreadState(true);
return true;
}
//Shorthand for SubscribeMouseMoveRelative
@ -118,18 +110,14 @@ namespace AutoHotInterception
/// <param name="block">Whether or not to block the movement</param>
/// <param name="callback">The callback to fire when the mouse moves</param>
/// <returns></returns>
public bool SubscribeMouseMoveRelative(int id, bool block, dynamic callback)
public void SubscribeMouseMoveRelative(int id, bool block, dynamic callback)
{
if (!IsValidDeviceId(true, id))
{
return false;
}
IsValidDeviceId(true, id);
_mouseMoveRelativeMappings[id] = new MappingOptions() { Block = block, Callback = callback };
_filteredDevices[id] = true;
SetFilterState(true);
SetThreadState(true);
return true;
}
/// <summary>
@ -140,18 +128,14 @@ namespace AutoHotInterception
/// <param name="block">Whether or not to block the movement</param>
/// <param name="callback">The callback to fire when the mouse moves</param>
/// <returns></returns>
public bool SubscribeMouseMoveAbsolute(int id, bool block, dynamic callback)
public void SubscribeMouseMoveAbsolute(int id, bool block, dynamic callback)
{
if (!IsValidDeviceId(true, id))
{
return false;
}
IsValidDeviceId(true, id);
_mouseMoveAbsoluteMappings[id] = new MappingOptions() { Block = block, Callback = callback };
_filteredDevices[id] = true;
SetFilterState(true);
SetThreadState(true);
return true;
}
#endregion
@ -164,12 +148,12 @@ namespace AutoHotInterception
/// <param name="id">The ID of the device</param>
/// <param name="callback">The callback to fire before and after each key or button press</param>
/// <returns></returns>
public bool SetContextCallback(int id, dynamic callback)
public void SetContextCallback(int id, dynamic callback)
{
SetFilterState(false);
if (id < 1 || id > 21)
if (id < 1 || id > 20)
{
return false;
throw new ArgumentOutOfRangeException(nameof(id), $"DeviceIds must be between 1 and 20");
}
_contextCallbacks[id] = callback;
@ -177,7 +161,6 @@ namespace AutoHotInterception
SetFilterState(true);
SetThreadState(true);
return true;
}
#endregion
@ -190,9 +173,9 @@ namespace AutoHotInterception
/// <param name="id">The ID of the Keyboard to send as</param>
/// <param name="code">The ScanCode to send</param>
/// <param name="state">The State to send (1 = pressed, 0 = released)</param>
public bool SendKeyEvent(int id, ushort code, int state)
public void SendKeyEvent(int id, ushort code, int state)
{
if (!IsValidDeviceId(false, id)) return false;
IsValidDeviceId(false, id);
var stroke = new ManagedWrapper.Stroke();
if (code > 255)
@ -203,7 +186,6 @@ namespace AutoHotInterception
stroke.key.code = code;
stroke.key.state = (ushort)(1 - state);
ManagedWrapper.Send(_deviceContext, id, ref stroke, 1);
return true;
}
/// <summary>
@ -213,9 +195,9 @@ namespace AutoHotInterception
/// <param name="btn"></param>
/// <param name="state"></param>
/// <returns></returns>
public bool SendMouseButtonEvent(int id, int btn, int state)
public void SendMouseButtonEvent(int id, int btn, int state)
{
if (!IsValidDeviceId(true, id)) return false;
IsValidDeviceId(true, id);
//var st = btn * 4;
//if (state == 0)
@ -229,7 +211,6 @@ namespace AutoHotInterception
stroke.mouse.state = (ushort)(1 << bit);
ManagedWrapper.Send(_deviceContext, id, ref stroke, 1);
return true;
}
public bool SendMouseMove(int id, int x, int y)
@ -246,7 +227,7 @@ namespace AutoHotInterception
/// <returns></returns>
public bool SendMouseMoveRelative(int id, int x, int y)
{
if (!IsValidDeviceId(true, id)) return false;
IsValidDeviceId(true, id);
var stroke = new ManagedWrapper.Stroke { mouse = { x = x, y = y, flags = (ushort)ManagedWrapper.MouseFlag.MouseMoveRelative } };
ManagedWrapper.Send(_deviceContext, id, ref stroke, 1);
@ -264,7 +245,7 @@ namespace AutoHotInterception
/// <returns></returns>
public bool SendMouseMoveAbsolute(int id, int x, int y)
{
if (!IsValidDeviceId(true, id)) return false;
IsValidDeviceId(true, id);
var stroke = new ManagedWrapper.Stroke { mouse = { x = x, y = y, flags = 1, state = 0, information = 0, rolling = 0} };
//var stroke = new ManagedWrapper.Stroke { mouse = { x = x, y = y, flags = (ushort)ManagedWrapper.MouseFlag.MouseMoveAbsolute} };
@ -276,6 +257,16 @@ namespace AutoHotInterception
#region Device Querying
public int GetKeyboardId(int vid, int pid, int instance = 1)
{
return GetDeviceId(false, vid, pid, instance);
}
public int GetMouseId(int vid, int pid, int instance = 1)
{
return GetDeviceId(true, vid, pid, instance);
}
/// <summary>
/// Tries to get Device ID from VID/PID
/// </summary>

@ -2,49 +2,23 @@
#Persistent
#include Lib\AutoHotInterception.ahk
VID := 0x04F2, PID := 0x0112
global AHI := new AutoHotInterception()
InterceptionWrapper := new AutoHotInterception()
global Interception := InterceptionWrapper.GetInstance()
keyboardId := AHI.GetKeyboardId(0x04F2, 0x0112)
devices := InterceptionWrapper.GetDeviceList()
if (!devices.Length()){
msgbox Device List Check failed
ExitApp
}
keyboardId := Interception.GetDeviceId(false, VID, PID)
if (keyboardId == 0){
MsgBox % "Could not find keyboard with VID " VID " PID " PID
ExitApp
}
AHI.SubscribeKey(keyboardId, GetKeySC("2"), true, Func("KeyEvent"))
result := Interception.SubscribeKey(keyboardId, GetKeySC("2"), true, Func("KeyEvent"))
if (result != -1){
msgbox Subscribe failed
ExitApp
}
result := Interception.SetContextCallback(keyboardId, Func("SetKb1Context"))
if (result != -1){
msgbox Context request failed
ExitApp
}
cm1 := AHI.CreateContextManager(keyboardId)
return
KeyEvent(state){
static ctrlCode := GetKeySC("Ctrl")
global keyboardId
;~ Interception.SendKeyEvent(keyboardId, ctrlCode, state)
;~ AHI.SendKeyEvent(keyboardId, ctrlCode, state)
ToolTip % "State: " state
}
SetKb1Context(state){
global isKeyboard1Active
Sleep 0 ; We seem to need this for hotstrings to work, not sure why
isKeyboard1Active := state
}
#if isKeyboard1Active
#if cm1.IsActive
::aaa::JACKPOT
1::
ToolTip % "KEY DOWN EVENT @ " A_TickCount

@ -2,32 +2,12 @@
#Persistent
#include Lib\AutoHotInterception.ahk
VID := 0x04F2, PID := 0x0112
InterceptionWrapper := new AutoHotInterception()
global Interception := InterceptionWrapper.GetInstance()
devices := InterceptionWrapper.GetDeviceList()
if (!devices.Length()){
msgbox Device List Check failed
ExitApp
}
keyboardId := Interception.GetDeviceId(false, VID, PID)
result := Interception.SetContextCallback(keyboardId, Func("SetKb1Context"))
if (result != -1){
msgbox Subscribe failed
ExitApp
}
AHI := new AutoHotInterception()
id1 := AHI.GetKeyboardId(0x04F2, 0x0112, 1)
cm1 := AHI.CreateContextManager(id1)
return
SetKb1Context(state){
global isKeyboard1Active
Sleep 0 ; We seem to need this for hotstrings to work, not sure why
isKeyboard1Active := state
}
#if isKeyboard1Active
#if cm1.IsActive
::aaa::JACKPOT
1::
ToolTip % "KEY DOWN EVENT @ " A_TickCount

@ -1,6 +1,8 @@
#include %A_LineFile%\..\CLR.ahk
class AutoHotInterception {
_contextManagers := {}
;_contextStates := {}
__New(cls := "Manager"){
dllName := "AutoHotInterception.dll"
dllFile := A_LineFile "\..\" dllName
@ -12,28 +14,113 @@ class AutoHotInterception {
asm := CLR_LoadLibrary(dllFile)
try {
this.Interception := asm.CreateInstance("AutoHotInterception." cls)
this.Instance := asm.CreateInstance("AutoHotInterception." cls)
}
catch {
MsgBox % dllName " failed to load`n`n" hintMessage
ExitApp
}
if (this.Interception.OkCheck() != "OK"){
if (this.Instance.OkCheck() != "OK"){
MsgBox % dllName " loaded but check failed!`n`n" hintMessage
ExitApp
}
}
GetInstance(){
return this.Interception
; --------------- Input Synthesis ----------------
SendKeyEvent(id, code, state){
this.Instance.SendKeyEvent(id, code, state)
}
SendMouseButtonEvent(id, btn, state){
this.Instance.SendMouseButtonEvent(id, btn, state)
}
SendMouseMove(id, x, y){
this.Instance.SendMouseMove(id, x, y)
}
SendMouseMoveRelative(id, x, y){
this.Instance.SendMouseMoveRelative(id, x, y)
}
SendMouseMoveAbsolute(id, x, y){
this.Instance.SendMouseMoveAbsolute(id, x, y)
}
*/
; --------------- Querying ------------------------
GetDeviceID(IsMouse, VID, PID, instance := 1){
static devType := {0: "Keyboard", 1: "Mouse"}
dev := this.Instance.GetDeviceId(IsMouse, VID, PID)
if (dev == 0){
MsgBox % "Could not get " devType[isMouse] " with VID " VID ", PID " PID ", Instance " instance
ExitApp
}
return dev
}
GetKeyboardID(VID, PID, instance := 1){
return this.GetDeviceId(false, VID, PID)
}
GetMouseID(VID, PID, instance := 1){
return this.GetDeviceId(true, VID, PID)
}
GetDeviceList(){
DeviceList := {}
arr := this.Interception.GetDeviceList()
arr := this.Instance.GetDeviceList()
for v in arr {
DeviceList[v.id] := { ID: v.id, VID: v.vid, PID: v.pid, IsMouse: v.IsMouse }
}
return DeviceList
}
; ---------------------- Subscription Mode ----------------------
SubscribeKey(id, code, block, callback){
this.Instance.SubscribeKey(id, code, block, callback)
}
SubscribeMouseButton(id, btn, block, callback){
this.Instance.SubscribeMouseButton(id, btn, block, callback)
}
SubscribeMouseMove(id, block, callback){
this.Instance.SubscribeMouseMove(id, block, callback)
}
SubscribeMouseMoveRelative(id, block, callback){
this.Instance.SubscribeMouseMoveRelative(id, block, callback)
}
SubscribeMouseMoveAbsolute(id, block, callback){
this.Instance.SubscribeMouseMoveAbsolute(id, block, callback)
}
; ------------- Context Mode ----------------
; Creates a context class to make it easy to turn on/off the hotkeys
CreateContextManager(id){
if (this._contextManagers.ContainsKey(id)){
Msgbox % "ID " id " already has a Context Manager"
ExitApp
}
cm := new this.ContextManager(this, id)
this._contextManagers[id] := cm
return cm
}
; Helper class for dealing with context mode
class ContextManager {
IsActive := 0
__New(parent, id){
this.parent := parent
this.id := id
result := this.parent.Instance.SetContextCallback(id, this.OnContextCallback.Bind(this))
}
OnContextCallback(state){
Sleep 0
this.IsActive := state
}
}
}

@ -12,7 +12,7 @@ filterMouseMove := 1
;~ global Monitor := AutoHotInterception_Init("InterceptionMonitor")
MonitorWrapper := new AutoHotInterception("Monitor")
global Monitor := MonitorWrapper.GetInstance()
global Monitor := MonitorWrapper.Instance
DeviceList := MonitorWrapper.GetDeviceList()

@ -2,25 +2,10 @@
#Persistent
#include Lib\AutoHotInterception.ahk
VID := 0x04F2, PID := 0x0112
AHI := new AutoHotInterception()
InterceptionWrapper := new AutoHotInterception()
global Interception := InterceptionWrapper.GetInstance()
devices := InterceptionWrapper.GetDeviceList()
if (!devices.Length()){
msgbox Device List Check failed
ExitApp
}
keyboardId := Interception.GetDeviceId(false, VID, PID)
result := Interception.SubscribeKey(keyboardId, GetKeySC("1"), true, Func("KeyEvent"))
if (result != -1){
msgbox Subscribe failed
ExitApp
}
keyboardId := AHI.GetKeyboardId(0x04F2, 0x0112)
AHI.SubscribeKey(keyboardId, GetKeySC("1"), true, Func("KeyEvent"))
return
KeyEvent(state){

Loading…
Cancel
Save