Skip to content

Commit

Permalink
Version 1.1.0.1, new configuration option, with it you can now select…
Browse files Browse the repository at this point in the history
… which Keys to Catch, all other Keys will be passed normally to OS
  • Loading branch information
sebeksd committed Mar 4, 2023
1 parent 175ea93 commit cdb742e
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 62 deletions.
39 changes: 0 additions & 39 deletions AutoHotkey_example.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,6 @@ MsgFunc(wParam, lParam, msg, hwnd)
OnUniqueKeyboard(wParam, lParam & 0xFF, (lParam & 0x100) > 0, (lParam & 0x200) > 0, (lParam & 0x400) > 0, (lParam & 0x800) > 0, (lParam & 0x1000) > 0, (lParam & 0x2000) > 0, (lParam & 0x4000) > 0, (lParam & 0x8000) > 0)
}

; do not modify
byte2hex(int)
{
; based on https://www.autohotkey.com/boards/viewtopic.php?t=3925
; used when doing "passthroug"
HEX_BYTE := 2
while (HEX_BYTE--)
{
n := (int >> (HEX_BYTE * 4)) & 0xf
h .= n > 9 ? chr(0x37 + n) : n
if (HEX_BYTE == 0 && HEX_BYTE//2 == 0)
h .= " "
}
return h
}

; do not modify
DoPassThrough(KeyboardNumber, VKeyCode, IsDown, WasDown, IsExtended, LeftCtrl, RightCtrl, LeftAlt, RightAlt, Shift)
{
VKeyCodeHex := byte2hex(VKeyCode)
PassThroughKey := "{vk" . VKeyCodeHex . "}"

if (LeftCtrl || RightCtrl)
PassThroughKey := "^" . PassThroughKey

if (LeftAlt || RightAlt)
PassThroughKey := "!" . PassThroughKey

if (Shift)
PassThroughKey := "+" . PassThroughKey

Send %PassThroughKey%
}

; KeyboardNumber - configured by you in MultiKB_For_AutoHotkey, this identify keyboard device
; VKeyCode - Key Code, https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
; IsDown - is key pressed or released, use "if (!IsDown)" if you like to register only key up, without this your code will be executed twice on key down and key up
Expand All @@ -66,11 +32,6 @@ OnUniqueKeyboard(KeyboardNumber, VKeyCode, IsDown, WasDown, IsExtended, LeftCtrl
; example, remove, keyboard 1 - "G" + RightCtrl + LeftAlt + Shift
if (KeyboardNumber = 1 and VKeyCode = 71 and RightCtrl and LeftAlt and Shift)
MsgBox "Test message"
; if you want to pass all other keys as normal then add code like below
else if (!IsDown)
{
DoPassThrough(KeyboardNumber, VKeyCode, IsDown, WasDown, IsExtended, LeftCtrl, RightCtrl, LeftAlt, RightAlt, Shift)
}

; ---> Add your code below <---

Expand Down
14 changes: 8 additions & 6 deletions MultiKB_For_AutoHotkey.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@
<PropertyGroup Condition="'$(Cfg_2_Win64)'!=''">
<AppEnableRuntimeThemes>true</AppEnableRuntimeThemes>
<AppDPIAwarenessMode>PerMonitorV2</AppDPIAwarenessMode>
<VerInfo_Build>6</VerInfo_Build>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.6;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Build>1</VerInfo_Build>
<VerInfo_Keys>CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.1.0.1;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.1.0.0;Comments=</VerInfo_Keys>
<VerInfo_MinorVer>1</VerInfo_MinorVer>
<VerInfo_Locale>11273</VerInfo_Locale>
</PropertyGroup>
<ItemGroup>
<DelphiCompile Include="$(MainSource)">
Expand Down Expand Up @@ -181,15 +183,15 @@
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="Win64\Debug\MultiKB_For_AutoHotkey.rsm" Configuration="Debug" Class="DebugSymbols">
<DeployFile LocalName="Win64\Debug\MultiKB_For_AutoHotkey.exe" Configuration="Debug" Class="ProjectOutput">
<Platform Name="Win64">
<RemoteName>MultiKB_For_AutoHotkey.rsm</RemoteName>
<RemoteName>MultiKB_For_AutoHotkey.exe</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
<DeployFile LocalName="Win64\Debug\MultiKB_For_AutoHotkey.exe" Configuration="Debug" Class="ProjectOutput">
<DeployFile LocalName="Win64\Debug\MultiKB_For_AutoHotkey.rsm" Configuration="Debug" Class="DebugSymbols">
<Platform Name="Win64">
<RemoteName>MultiKB_For_AutoHotkey.exe</RemoteName>
<RemoteName>MultiKB_For_AutoHotkey.rsm</RemoteName>
<Overwrite>true</Overwrite>
</Platform>
</DeployFile>
Expand Down
Binary file modified MultiKB_For_AutoHotkey.res
Binary file not shown.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ or by "logging" key presses in AutoHotkey like this:

MsgBox % "Keyboard: " . KeyboardNumber . " Key: " . VKeyCode

#### Additional options (availaible only in Configuration.ini) ####
Options will be added to Configuration file on Application close (when updating from older version run this app and close it).
CatchAll - default 1, Catch all keys on selected Keyboard without passing them to OS
CatchVKCodes - default [empty], list of VKCodes to Catch (only if CatchAll=0), only thoes Keys will be Catched and passed to AutoHotkey, those Keys will not go to OS, list should be splited using coma, correct Codes are availaible in link in section above

Example (Catch only Numpad 0-9):
CatchAll=0
CatchVKCodes=96,97,98,99,100,101,102,103,104,105

## Building from source ##

To build this app you need Delphi IDE, it should work on most of Delphi versions (XE+), it doesn't require any external components or tools. I'm using Delphi 10.4 community edition (which is free).
Expand Down
54 changes: 52 additions & 2 deletions src/Configuration.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface

uses
System.SyncObjs, Devices, System.IniFiles;
System.SyncObjs, Devices, System.IniFiles, System.Generics.Collections;

const
gcConfigurationDir = 'MultiKeyboardForAutoHotkey\';
Expand All @@ -28,6 +28,9 @@ TConfiguration = class
procedure SetDefaults;
function GetIsChanged: Boolean;

procedure VKCodesStringListToList(const pList: TList<Integer>; const lVKCodesStr: string);
function ListToVKCodesStringList(const lList: TList<Integer>): string;

protected // unsafe, require Lock
function Load(const lPath: string): Boolean;
function Save(const lPath: string): Boolean;
Expand All @@ -49,7 +52,7 @@ TConfiguration = class
implementation

uses
System.SysUtils, Winapi.Windows, SHFolder, Vcl.Dialogs;
System.SysUtils, Winapi.Windows, SHFolder, Vcl.Dialogs, Classes;

function GetAppDataLocalPath: string;
var
Expand Down Expand Up @@ -121,6 +124,18 @@ function TConfiguration.GetIsChanged: Boolean;
end;
end;

function TConfiguration.ListToVKCodesStringList(const lList: TList<Integer>): string;
begin
Result := '';
for var x: Integer := 0 to lList.Count - 1 do
begin
if x > 0 then
Result := Result + ',';

Result := Result + IntToStr(lList[x]);
end;
end;

function TConfiguration.Load(const lPath: string): Boolean;
var
lInitFile: TIniFile;
Expand Down Expand Up @@ -148,6 +163,13 @@ function TConfiguration.Load(const lPath: string): Boolean;
lDevice.Number := lInitFile.ReadInteger(lSectionName, 'Number', -1);
lDevice.SystemId := lInitFile.ReadString(lSectionName, 'SystemId', '');

lDevice.CatchAll := lInitFile.ReadBool(lSectionName, 'CatchAll', True);
if not lDevice.CatchAll then
begin
var lCatchListStr: string := lInitFile.ReadString(lSectionName, 'CatchVKCodes', '');
VKCodesStringListToList(lDevice.CatchList, lCatchListStr);
end;

// validate before add
if not lDevice.IsValid() then
lDevice.Free
Expand Down Expand Up @@ -201,6 +223,9 @@ function TConfiguration.Save(const lPath: string): Boolean;
lInitFile.WriteString(lSectionName, 'Name', fConfiguredDeviceList[x].Name);
lInitFile.WriteInteger(lSectionName, 'Number', fConfiguredDeviceList[x].Number);
lInitFile.WriteString(lSectionName, 'SystemId', fConfiguredDeviceList[x].SystemId);

lInitFile.WriteBool(lSectionName, 'CatchAll', fConfiguredDeviceList[x].CatchAll);
lInitFile.WriteString(lSectionName, 'CatchVKCodes', ListToVKCodesStringList(fConfiguredDeviceList[x].CatchList));
end;
lInitFile.UpdateFile;
finally
Expand Down Expand Up @@ -246,6 +271,31 @@ procedure TConfiguration.UpdateDeviceList(const lDeviceList: TDeviceList; const
end;
end;

procedure TConfiguration.VKCodesStringListToList(const pList: TList<Integer>; const lVKCodesStr: string);
begin
if lVKCodesStr = '' then
begin
pList.Clear;
Exit;
end;

var lStringList: TStringList := TStringList.Create;
try
lStringList.StrictDelimiter := True;
lStringList.Delimiter := ',';
lStringList.DelimitedText := lVKCodesStr;

for var x: Integer := 0 to lStringList.Count - 1 do
begin
var lVKCode: Integer := StrToIntDef(Trim(lStringList[x]), -1);
if (lVKCode > -1) then
pList.Add(lVKCode);
end;
finally
FreeAndNil(lStringList);
end;
end;

initialization
gConfiguration := TConfiguration.Create;

Expand Down
45 changes: 41 additions & 4 deletions src/Devices.pas
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ interface

TDevice = class(TPersistent)
private
fEnabled: Boolean; // user provided information
fName: string; // user provided information
fNumber: Integer; // user provided information
// user provided information
fEnabled: Boolean;
fName: string;
fNumber: Integer;

fCatchAll: Boolean;
fCatchList: TList<Integer>;
// end user provided information

fSystemId: string; // unique for device and port
fHandle: THandle; // unique "at this moment"
Expand All @@ -28,7 +33,8 @@ TDevice = class(TPersistent)
Shift: TKeyDirection;
end;

constructor Create;
constructor Create; reintroduce;
destructor Destroy; override;

function Clone: TDevice;
procedure Assign(Source: TPersistent); override;
Expand All @@ -38,11 +44,15 @@ TDevice = class(TPersistent)
function IsDeviceConfigured: Boolean;
function IsDeviceEnabled: Boolean;
function IsValid: Boolean;
function IsVKCodeOnCatchList(const lVKCode: Byte): Boolean;

property Enabled: Boolean read fEnabled write fEnabled;
property Name: string read fName write fName;
property Number: Integer read fNumber write fNumber;

property CatchAll: Boolean read fCatchAll write fCatchAll;
property CatchList: TList<Integer> read fCatchList;

property SystemId: string read fSystemId write fSystemId;
property Handle: THandle read fHandle write fHandle;
end;
Expand Down Expand Up @@ -223,6 +233,11 @@ procedure TDevice.Assign(Source: TPersistent);
fEnabled := lDevice.Enabled;
fName := lDevice.Name;
fNumber := lDevice.Number;

fCatchAll := lDevice.fCatchAll;
for var x: Integer := 0 to lDevice.fCatchList.Count - 1 do
fCatchList.Add(lDevice.fCatchList[x]);

fSystemId := lDevice.SystemId;
fHandle := lDevice.Handle;
end
Expand All @@ -238,6 +253,11 @@ function TDevice.Clone: TDevice;

constructor TDevice.Create;
begin
inherited;

fCatchAll := True;
fCatchList := TList<Integer>.Create();

State.LeftAlt := kdUp;
State.RightAlt := kdUp;
State.LeftCtrl := kdUp;
Expand All @@ -247,6 +267,12 @@ constructor TDevice.Create;
fHandle := INVALID_HANDLE_VALUE;
end;

destructor TDevice.Destroy;
begin
FreeAndNil(fCatchList);
inherited;
end;

procedure TDevice.FillUserData(const lDevice: TDevice);
begin
// this data was provided by user
Expand All @@ -270,4 +296,15 @@ function TDevice.IsValid: Boolean;
Result := IsDeviceConfigured() and (SystemId <> '');
end;

function TDevice.IsVKCodeOnCatchList(const lVKCode: Byte): Boolean;
begin
for var x: Integer := 0 to fCatchList.Count - 1 do
begin
if lVKCode = fCatchList[x] then
Exit(True);
end;

Result := False;
end;

end.
23 changes: 12 additions & 11 deletions src/Engine.pas
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ procedure TEngine.OnHookMessage(var Message: TMessage);
lKS: TKeyStroke;
lDoBlock: Boolean;
begin
// hook message has no device ID so we need RawInput for that
lDoBlock := False;

// hook message has no device ID so we need RawInput for that
fLock.Enter;
try
lKS := ConvertHookMessageToKeyStroke(Message.WParam, Message.LParam);
Expand All @@ -288,19 +290,18 @@ procedure TEngine.OnHookMessage(var Message: TMessage);

if Assigned(lKS.Device) then
begin
// uopdate current state of special control keys for this device
// update current state of special control keys for this device
UpdateControlKeyStateForDevice(lKS.Device, lKS);

// send keystroke to AutoHotkey
KeyStrokeToAutoHotkey(lKS);
if lKS.IsOnCatchList() then
begin
// send keystroke to AutoHotkey
KeyStrokeToAutoHotkey(lKS);

lDoBlock := True;
end
else
lDoBlock := False;
end
else
lDoBlock := False;
lDoBlock := True;
end;
end;
end;
finally
fLock.Leave;
end;
Expand Down
9 changes: 9 additions & 0 deletions src/KeyStroke.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ TKeyStroke = record
IsExtendedKey: Boolean;

function IsEqual(const lKeyStroke: TKeyStroke): Boolean;
function IsOnCatchList(): Boolean;
end;

TKeyLogItem = record
Expand Down Expand Up @@ -72,6 +73,14 @@ function TKeyStroke.IsEqual(const lKeyStroke: TKeyStroke): Boolean;
Result := (VKeyCode = lKeyStroke.VKeyCode) and (Direction = lKeyStroke.Direction);
end;

function TKeyStroke.IsOnCatchList: Boolean;
begin
if not Assigned(Device) then
Exit(False);

Result := Device.CatchAll or Device.IsVKCodeOnCatchList(VKeyCode);
end;

end.


0 comments on commit cdb742e

Please sign in to comment.