Skip to content

Commit

Permalink
keycode
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Oct 10, 2023
1 parent eed380a commit 318220d
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 298 deletions.
3 changes: 3 additions & 0 deletions Source/colormath/simba.colormath.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ interface
function AsString: String;
end;

TColor = Graphics.TColor;
PColor = ^TColor;

TColorHelper = type helper for TColor
function ToBGRA: TColorBGRA;
function ToRGB: TColorRGB;
Expand Down
11 changes: 11 additions & 0 deletions Source/components/simba.component_menubar.pas
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ TSimbaMainMenuBar = class(TCustomControl)
function GetMenus: TPopupMenuArray;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

property Menus: TPopupMenuArray read GetMenus;
procedure AddMenu(Title: String; APopupMenu: TPopupMenu);
Expand Down Expand Up @@ -235,6 +236,8 @@ procedure TSimbaMainMenuBar.MouseLeave;

procedure TSimbaMainMenuBar.DoMenuClose(Sender: TObject);
begin
if (FTrackTimer = nil) then
Exit;
FTrackTimer.Enabled := False;

Application.QueueAsyncCall(@ClearPopupIndex, 0);
Expand Down Expand Up @@ -286,6 +289,14 @@ constructor TSimbaMainMenuBar.Create(AOwner: TComponent);
CalculateSizes();
end;

destructor TSimbaMainMenuBar.Destroy;
begin
Application.RemoveAsyncCalls(Self);
FTrackTimer := nil;

inherited Destroy();
end;

procedure TSimbaMainMenuBar.AddMenu(Title: String; APopupMenu: TPopupMenu);
var
I: Integer;
Expand Down
10 changes: 4 additions & 6 deletions Source/components/simba.component_statusbar.pas
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TSimbaStatusBar = class(TCustomControl)
procedure Paint; override;
procedure PaintPanel(Index: Integer);

procedure InvalidatePanel(Index: Integer);
procedure InvalidatePanelASync(Data: PtrInt);
function PanelRect(Index: Integer): TRect;

procedure CheckIndex(Index: Integer);
Expand Down Expand Up @@ -78,10 +78,9 @@ procedure TSimbaStatusBar.SetPanelText(Index: Integer; Value: String);
begin
if (FPanelText[Index] = Value) then
Exit;

FPanelText[Index] := Value;

InvalidatePanel(Index);
Application.QueueAsyncCall(@InvalidatePanelASync, Index);
end;

procedure TSimbaStatusBar.SetPanelTextMeasure(Index: Integer; Value: String);
Expand Down Expand Up @@ -194,11 +193,11 @@ procedure TSimbaStatusBar.PaintPanel(Index: Integer);
Canvas.TextRect(R, R.Left + 5, R.Top, FPanelText[Index], Style);
end;

procedure TSimbaStatusBar.InvalidatePanel(Index: Integer);
procedure TSimbaStatusBar.InvalidatePanelASync(Data: PtrInt);
var
R: TRect;
begin
R := PanelRect(Index);
R := PanelRect(Data);

InvalidateRect(Handle, @R, False);
end;
Expand Down Expand Up @@ -226,4 +225,3 @@ function TSimbaStatusBar.PanelRect(Index: Integer): TRect;
end;

end.

4 changes: 2 additions & 2 deletions Source/imagebox/simba.imagebox.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
interface

uses
Classes, SysUtils, Forms, Controls, Graphics, ComCtrls, LCLType,
Classes, SysUtils, Forms, Controls, Graphics, GraphType, ComCtrls, LCLType,
simba.mufasatypes, simba.image, simba.dtm, simba.imagebox_image,
simba.colormath;

Expand Down Expand Up @@ -112,7 +112,7 @@ TSimbaImageBox = class(TWinControl)
implementation

uses
Math, GraphType, LCLIntf,
Math, LCLIntf,
simba.finder, simba.image_lazbridge, simba.windowhandle;

procedure TSimbaImageBox_ScrollBox.GetPreferredSize(var PreferredWidth, PreferredHeight: integer; Raw: boolean; WithThemeSpace: boolean);
Expand Down
24 changes: 17 additions & 7 deletions Source/script/imports/simba/simba.import_input.pas
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,23 @@ procedure _LapeInput_KeySend(const Params: PParamArray); LAPE_WRAPPER_CALLING_CO
end;

(*
TInput.CharToKeyCode
~~~~~~~~~~~~~~~~~~~~
> function TInput.CharToKeyCode(C: Char): EKeyCode;
TInput.KeyCodeFromChar
~~~~~~~~~~~~~~~~~~~~~~
> function TInput.KeyCodeFromChar(C: Char): EKeyCode;
*)
procedure _LapeInput_KeyCodeFromChar(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PKeyCode(Result)^ := PSimbaInput(Params^[0])^.KeyCodeFromChar(PChar(Params^[1])^);
end;

(*
TInput.KeyModifiersFromChar
~~~~~~~~~~~~~~~~~~~~~~~~~~~
> procedure TInput.KeyModifiersFromChar(Key: Char; out Shift, Ctrl, Alt: Boolean);
*)
procedure _LapeInput_CharToKeyCode(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
procedure _LapeInput_KeyModifiersFromChar(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
begin
PKeyCode(Result)^ := PSimbaInput(Params^[0])^.CharToKeyCode(PChar(Params^[1])^);
PSimbaInput(Params^[0])^.KeyModifiersFromChar(PChar(Params^[1])^, PBoolean(Params^[2])^, PBoolean(Params^[3])^, PBoolean(Params^[4])^);
end;

(*
Expand Down Expand Up @@ -826,13 +836,13 @@ procedure ImportInput(Compiler: TSimbaScript_Compiler);
addInputMethod('procedure TInput.MouseScroll(Scrolls: Integer)', @_LapeInput_MouseScroll);
addInputMethod('procedure TInput.MouseMove(Dest: TPoint);', @_LapeInput_MouseMove);

addInputMethod('function TInput.KeyCodeFromChar(C: Char): EKeyCode;', @_LapeInput_KeyCodeFromChar);
addInputMethod('procedure TInput.KeyModifiersFromChar(Key: Char; out Shift, Ctrl, Alt: Boolean);', @_LapeInput_KeyModifiersFromChar);
addInputMethod('procedure TInput.KeySend(Text: String)', @_LapeInput_KeySend);
addInputMethod('procedure TInput.KeyPress(Key: EKeyCode)', @_LapeInput_KeyPress);
addInputMethod('procedure TInput.KeyDown(Key: EKeyCode)', @_LapeInput_KeyDown);
addInputMethod('procedure TInput.KeyUp(Key: EKeyCode)', @_LapeInput_KeyUp);
addInputMethod('function TInput.KeyPressed(Key: EKeyCode): Boolean', @_LapeInput_KeyPressed);

addInputMethod('function TInput.CharToKeyCode(C: Char): EKeyCode', @_LapeInput_CharToKeyCode);
end;

ImportASyncMouse(Compiler);
Expand Down
2 changes: 1 addition & 1 deletion Source/simba.darwin_axui.pas
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function AXUI_GetWindowInfo(PID: UInt32): TAXUIWindowInfo;
end;

initialization
SimbaIDEInitialization_AddBeforeShow(@RequestAccessibility, 'RequestAccessibility');
// SimbaIDEInitialization_AddBeforeShow(@RequestAccessibility, 'RequestAccessibility');

CreateCFStrings();

Expand Down
3 changes: 1 addition & 2 deletions Source/simba.ide_events.pas
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ SimbaIDEEvents = class
class procedure CallOnEditorModified(Sender: TObject);
class procedure CallOnMouseLoggerChange(Sender: TObject);


// Called every 500ms from a script instance while a script is running.
// Called on a seperate thread - synchronize if doing GUI stuff!
// Sender = TSimbaScriptInstance
class procedure CallOnScriptRunning(Sender: TObject);
class procedure RegisterMethodOnScriptRunning(Proc: TNotifyEvent);
class procedure UnRegisterMethodOnScriptRunning(Proc: TNotifyEvent);


class procedure RegisterMethodOnScriptStateChange(Proc: TNotifyEvent);
class procedure RegisterMethodOnScriptTabChange(Proc: TNotifyEvent);
class procedure RegisterOnBeforeTabChange(Proc: TNotifyEvent);
Expand Down
129 changes: 80 additions & 49 deletions Source/simba.input.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ interface
EMouseEventType = (TELEPORT, MOVING, CLICK, DOWN, UP);
{$SCOPEDENUMS OFF}
const
DEFAULT_KEY_PRESS_MIN = 30;
DEFAULT_KEY_PRESS_MAX = 140;
DEFAULT_KEY_PRESS_MIN = 25;
DEFAULT_KEY_PRESS_MAX = 130;

DEFAULT_CLICK_MIN = 40;
DEFAULT_CLICK_MAX = 220;
Expand Down Expand Up @@ -82,14 +82,14 @@ interface
procedure MouseUp(Button: EMouseButton);
procedure MouseScroll(Scrolls: Integer);

function KeyCodeFromChar(C: Char): EKeyCode;
procedure KeyModifiersFromChar(Key: Char; out Shift, Ctrl, Alt: Boolean);
procedure KeySend(Text: String);
procedure KeyPress(Key: EKeyCode);
procedure KeyDown(Key: EKeyCode);
procedure KeyUp(Key: EKeyCode);
function KeyPressed(Key: EKeyCode): Boolean;

function CharToKeyCode(C: Char): EKeyCode;

function AddOnMouseTeleport(Event: TMouseTeleportEvent): TMouseTeleportEvent;
function AddOnMouseMoving(Event: TMouseMovingEvent): TMouseMovingEvent;
function AddOnMouseDown(Event: TMouseButtonEvent): TMouseButtonEvent;
Expand Down Expand Up @@ -130,7 +130,7 @@ TSimbaASyncMouse = class(TSimbaBaseClass)
implementation

uses
simba.math, simba.nativeinterface, simba.random, simba.threading;
simba.math, simba.nativeinterface, simba.random, simba.threading, simba.target_eios;

procedure TSimbaInput.ClearEvent(EventType: EMouseEventType);
begin
Expand Down Expand Up @@ -386,12 +386,86 @@ procedure TSimbaInput.MouseScroll(Scrolls: Integer);
Target.MouseScroll(Scrolls);
end;

function TSimbaInput.KeyCodeFromChar(C: Char): EKeyCode;
begin
Result := Target.KeyCodeFromChar(C);
end;

procedure TSimbaInput.KeyModifiersFromChar(Key: Char; out Shift, Ctrl, Alt: Boolean);
begin
Target.KeyModifiersFromChar(Key, Shift, Ctrl, Alt);
end;

procedure TSimbaInput.KeySend(Text: String);
var
ShiftDown, CtrlDown, AltDown: Boolean;

procedure HandleModifier(const Needed: Boolean; var isDown: Boolean; const KeyCode: EKeyCode);
begin
if (Needed = isDown) then
Exit;
isDown := Needed;

if Needed then
Target.KeyDown(KeyCode, GetRandomKeyPressTime())
else
Target.KeyUp(KeyCode, GetRandomKeyPressTime());
end;

type
TKeyCodeMap = array[Char] of record
KeyCode: EKeyCode;
ShiftNeeded, CtrlNeeded, AltNeeded: Boolean;
end;
var
I: Integer;
KeyCodes: TKeyCodeMap;
begin
// eios is deprecated but handle it how previous versions used.
if Target.IsEIOSTarget() then
begin
Target.EIOS.SendString(Target.EIOS.Target, PChar(Text), GetRandomKeyPressTime(), GetRandomKeyPressTime());
Exit;
end;

KeyCodes := Default(TKeyCodeMap);

for I := 1 to Length(Text) do
Target.KeySend(Text[I], GetRandomKeyPressTime() div 2, GetRandomKeyPressTime() div 2, GetRandomKeyPressTime() div 2, GetRandomKeyPressTime() div 2);
with KeyCodes[Text[I]] do
begin
if (KeyCode = EKeyCode.UNKNOWN) then
begin
KeyCode := Target.KeyCodeFromChar(Text[I]);

Target.KeyModifiersFromChar(
Text[I],
ShiftNeeded,
CtrlNeeded,
AltNeeded
);
end;
end;

ShiftDown := False;
CtrlDown := False;
AltDown := False;

try
for I := 1 to Length(Text) do
with KeyCodes[Text[I]] do
begin
HandleModifier(ShiftNeeded, ShiftDown, EKeyCode.SHIFT);
HandleModifier(CtrlNeeded, CtrlDown, EKeyCode.CONTROL);
HandleModifier(AltNeeded, AltDown, EKeyCode.MENU);

Target.KeyDown(KeyCode, GetRandomKeyPressTime());
Target.KeyUp(KeyCode, GetRandomKeyPressTime());
end;
finally
HandleModifier(False, ShiftDown, EKeyCode.SHIFT);
HandleModifier(False, CtrlDown, EKeyCode.CONTROL);
HandleModifier(False, AltDown, EKeyCode.MENU);
end;
end;

procedure TSimbaInput.KeyPress(Key: EKeyCode);
Expand All @@ -416,49 +490,6 @@ function TSimbaInput.KeyPressed(Key: EKeyCode): Boolean;
Result := Target.KeyPressed(Key);
end;

function TSimbaInput.CharToKeyCode(C: Char): EKeyCode;
begin
case C of
'0'..'9': Result := EKeyCode(Ord(EKeyCode.NUM_0) + Ord(C) - Ord('0'));
'a'..'z': Result := EKeyCode(Ord(EKeyCode.A) + Ord(C) - Ord('a'));
'A'..'Z': Result := EKeyCode(Ord(EKeyCode.A) + Ord(C) - Ord('A'));
#34, #39: Result := EKeyCode.OEM_7;
#32: Result := EKeyCode.SPACE;
'!': Result := EKeyCode.NUM_1;
'#': Result := EKeyCode.NUM_3;
'$': Result := EKeyCode.NUM_4;
'%': Result := EKeyCode.NUM_5;
'&': Result := EKeyCode.NUM_7;
'(': Result := EKeyCode.NUM_9;
')': Result := EKeyCode.NUM_0;
'*': Result := EKeyCode.NUM_8;
'+': Result := EKeyCode.ADD;
',': Result := EKeyCode.OEM_COMMA;
'-': Result := EKeyCode.OEM_MINUS;
'.': Result := EKeyCode.OEM_PERIOD;
'/': Result := EKeyCode.OEM_2;
':': Result := EKeyCode.OEM_1;
';': Result := EKeyCode.OEM_1;
'<': Result := EKeyCode.OEM_COMMA;
'=': Result := EKeyCode.ADD;
'>': Result := EKeyCode.OEM_PERIOD;
'?': Result := EKeyCode.OEM_2;
'@': Result := EKeyCode.NUM_2;
'[': Result := EKeyCode.OEM_4;
'\': Result := EKeyCode.OEM_5;
']': Result := EKeyCode.OEM_6;
'^': Result := EKeyCode.NUM_6;
'_': Result := EKeyCode.OEM_MINUS;
'`': Result := EKeyCode.OEM_3;
'{': Result := EKeyCode.OEM_4;
'|': Result := EKeyCode.OEM_5;
'}': Result := EKeyCode.OEM_6;
'~': Result := EKeyCode.OEM_3;
else
Result := EKeyCode.UNKNOWN;
end;
end;

procedure TSimbaInput.CallOnTeleportEvents(P: TPoint);
var
Method: TMethod;
Expand Down
Loading

0 comments on commit 318220d

Please sign in to comment.