From a68d70d456dcdecd52ed72cd3691cf24d75a135c Mon Sep 17 00:00:00 2001 From: Wizzup? Date: Sun, 8 Nov 2009 16:23:04 +0000 Subject: [PATCH] Added KeyUp/KeyDown/KeyPress and SendText. Should work on Windows as well on Linux. git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@211 3f818213-9676-44b0-a9b4-5e4c4e03d09d --- Projects/MufasaTests/project1.lpi | 112 ++++++++++----------- Projects/MufasaTests/project1.lpr | 4 +- Units/MMLAddon/PSInc/Wrappers/keyboard.inc | 42 ++++++++ Units/MMLAddon/PSInc/pscompile.inc | 6 ++ Units/MMLAddon/mmlpsthread.pas | 2 +- Units/MMLCore/input.pas | 53 ++++++++-- 6 files changed, 151 insertions(+), 68 deletions(-) create mode 100644 Units/MMLAddon/PSInc/Wrappers/keyboard.inc diff --git a/Projects/MufasaTests/project1.lpi b/Projects/MufasaTests/project1.lpi index 6fa3317..9ea1113 100644 --- a/Projects/MufasaTests/project1.lpi +++ b/Projects/MufasaTests/project1.lpi @@ -11,7 +11,7 @@ - + @@ -35,8 +35,8 @@ - - + + @@ -90,8 +90,8 @@ - - + + @@ -109,7 +109,7 @@ - + @@ -181,124 +181,124 @@ - - - - - - + + - - + + + + + + - + - + - - - - - - + + - - + + - - + + - - + + + + + + - + - + - + - - + + - + - + - + - - - - - - + + + + + + - + - + - - - - + + + + - + - - - - - - + + + + + + @@ -306,7 +306,7 @@ - + diff --git a/Projects/MufasaTests/project1.lpr b/Projects/MufasaTests/project1.lpr index 810745a..2836824 100644 --- a/Projects/MufasaTests/project1.lpr +++ b/Projects/MufasaTests/project1.lpr @@ -93,7 +93,9 @@ begin writeln('Time: ' + FloatToStr(((GetTickCount - Time) / (i + 1)))); writeln(C.MOCR.GetUpTextAt(0,0)); } - C.MInput.ClickMouse(5,5, mouse_Left); + //C.MInput.ClickMouse(5,5, mouse_Left); + sleep(2000); + C.MInput.SendText('a'); C.Free; bmp.OnDestroy:=nil; diff --git a/Units/MMLAddon/PSInc/Wrappers/keyboard.inc b/Units/MMLAddon/PSInc/Wrappers/keyboard.inc new file mode 100644 index 0000000..f106cbf --- /dev/null +++ b/Units/MMLAddon/PSInc/Wrappers/keyboard.inc @@ -0,0 +1,42 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venetiƫ and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see . + + See the file COPYING, included in this distribution, + for details about the copyright. + + Keyboard.inc for the Mufasa Macro Library +} + +procedure KeyDown(key: Word); +begin + CurrThread.Client.MInput.KeyDown(key); +end; + +procedure KeyUp(key: Word); +begin + CurrThread.Client.MInput.KeyUp(key); +end; + +procedure SendKeys(s: string); +begin + CurrThread.Client.MInput.SendText(s); +end; + +procedure PressKey(key: Word); +begin + CurrThread.Client.MInput.PressKey(key); +end; diff --git a/Units/MMLAddon/PSInc/pscompile.inc b/Units/MMLAddon/PSInc/pscompile.inc index c3c6acf..caae63d 100644 --- a/Units/MMLAddon/PSInc/pscompile.inc +++ b/Units/MMLAddon/PSInc/pscompile.inc @@ -97,6 +97,12 @@ Sender.AddFunction(@SetColorToleranceSpeed, 'procedure SetColorToleranceSpeed(ct Sender.AddFunction(@MoveMouse, 'procedure MoveMouse(x, y: integer);'); Sender.AddFunction(@GetMousePos, 'procedure GetMousePos(var x, y: integer);'); +{Keyboard} +Sender.AddFunction(@KeyDown, 'procedure KeyDown(key: Word);'); +Sender.AddFunction(@KeyUp, 'procedure KeyUp(key: Word);'); +Sender.AddFunction(@PressKey, 'procedure PressKey(key: Word);'); +Sender.AddFunction(@SendKeys, 'procedure SendKeys(s: string);'); + { OCR} Sender.AddFunction(@rs_GetUpText, 'function rs_GetUpText: string;'); diff --git a/Units/MMLAddon/mmlpsthread.pas b/Units/MMLAddon/mmlpsthread.pas index 69c4066..0f35377 100644 --- a/Units/MMLAddon/mmlpsthread.pas +++ b/Units/MMLAddon/mmlpsthread.pas @@ -157,7 +157,6 @@ begin SyncInfo:= TheSyncInfo; SetLength(PluginsToLoad,0); Client := TClient.Create; - PSScript := TPSScript.Create(nil); PSScript.UsePreProcessor:= True; PSScript.OnNeedFile := @RequireFile; @@ -196,6 +195,7 @@ end; {$I PSInc/Wrappers/colour.inc} {$I PSInc/Wrappers/math.inc} {$I PSInc/Wrappers/mouse.inc} +{$I PSInc/Wrappers/keyboard.inc} {$I PSInc/Wrappers/dtm.inc} {$I PSInc/Wrappers/ocr.inc} diff --git a/Units/MMLCore/input.pas b/Units/MMLCore/input.pas index 4e0ab9e..b686969 100644 --- a/Units/MMLCore/input.pas +++ b/Units/MMLCore/input.pas @@ -30,11 +30,13 @@ interface uses Classes, SysUtils, mufasatypes, // for common mufasa types - windowutil // for mufasa window utils + windowutil, // for mufasa window utils {$IFDEF LINUX} - ,ctypes,x, xlib,xtest{,keysym} // for X* stuff - // do non silent keys with XTest. - {$ENDIF}; + ctypes,x, xlib,xtest, // for X* stuff + // do non silent keys/mouse with XTest / TKeyInput. + {Later on we should use xdotool, as it allows silent input} + {$ENDIF} + MouseAndKeyInput, KeyInputIntf, lclintf; type TMInput = class(TObject) constructor Create(Client: TObject); @@ -46,8 +48,10 @@ type procedure MouseButtonActionSilent(x,y : integer; mClick: TClickType; mPress: TMousePress); procedure ClickMouse(X, Y: Integer; mClick: TClickType); - procedure KeyUp(key: Integer); - procedure KeyDown(key: Integer); + procedure KeyUp(key: Word); + procedure KeyDown(key: Word); + procedure PressKey(key: Word); + procedure SendText(text: string); // Not used yet. procedure SetSilent(_Silent: Boolean); @@ -63,13 +67,14 @@ type private // Not used yet. Silent: Boolean; + //KeyInput: TKeyInput; end; implementation uses - Client{$IFDEF MSWINDOWS},windows{$ENDIF}; + Client,{$IFDEF MSWINDOWS}windows {$ELSE}lcltype{$ENDIF}; {$IFDEF MSWINDOWS} type @@ -123,24 +128,52 @@ constructor TMInput.Create(Client: TObject); begin inherited Create; Self.Client := Client; + //Self.KeyInput := KeyInput; + end; destructor TMInput.Destroy; begin + //Self.KeyInput := nil; inherited; end; -procedure TMInput.KeyUp(key: Integer); +procedure TMInput.KeyUp(key: Word); begin - + {Self.}KeyInput.Up(Key); end; -procedure TMInput.KeyDown(key: Integer); +procedure TMInput.KeyDown(key: Word); begin + {Self.}KeyInput.Down(Key); +end; +procedure TMInput.PressKey(key: Word); +begin + Self.KeyDown(key); + Self.KeyUp(key); +end; + +{ No using VkKeyScan } +function GetSimpleKeyCode(c: char): word; + +begin + //result := ord(UpCase(c)); + c := lowerCase(c); + if ((c >= 'a') and (c <= 'z')) then + Exit(VK_A + (Byte(c) - 97)); + Raise Exception.CreateFMT('GetSimpleKeyCode - char is not in A..z',[]); +end; + +procedure TMInput.SendText(text: string); +var + i: integer; +begin + for i := 1 to length(text) do + Self.PressKey(GetSimpleKeyCode(text[i])); end; procedure TMInput.GetMousePos(var X, Y: Integer);