-
Notifications
You must be signed in to change notification settings - Fork 3
/
SendKey32.pas
1393 lines (1211 loc) · 42.9 KB
/
SendKey32.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{*******************************************************}
{ }
{ 08/2020 MaxiDonkey Library }
{ }
{*******************************************************}
unit SendKey32;
interface
uses
SysUtils, classes, Windows, Messages, Dialogs, Forms, KeysDef, uRegistry,
StrUtils, System.Types;
type
{For KeyBoardState}
TByteSet = Set of byte;
{...............................................................................
NOTE
Instancier la classe TPushKeyCombine = class; si les appels sont fréquents
afin d'éviter l'appel à la fonction de classe (sporadique)
...............................................................................}
procedure Wait(Time: Cardinal = 15);
procedure WaitForKey(Time: Cardinal = 250);
{ --- Buffer for working with PChar's : Note ** 1 caractère + #0 sinon ce n'est pas nécessaire --- }
function SendKeys(SendKeysString : PWideChar; Wait : Boolean) : Boolean; overload;
{ --- Appel que pour une combinaison de touches
warning : iKey := VK_LEFT ou iKey := VkKeyScan('A')
note : Specials permet de distinguer les alt, ctrl, shift gauche et droit --- }
procedure SendKey(iKey: Smallint; Tms: Cardinal; Specials: TSpecials = []); overload;
{ --- Combinaison Win+Alpha --- }
procedure SendKeyWin(iKey: Smallint; Tms: Cardinal);
{ --- Alt + xxxx ex : ³ âr alt+0179 --- }
procedure SendKey(sCode: string); overload;
{ --- Mouse --- }
function GetMousePosition: TPoint;
procedure SetMousePosition(const Position: TPoint); overload;
procedure SetMousePosition(x,y: Integer); overload;
procedure MouseLeftClic; overload;
procedure MouseLeftClic(Tms: Cardinal; Shift: TShiftState); overload;
procedure MouseRightClic; overload;
procedure MouseRightClic(Tms: Cardinal; Shift: TShiftState); overload;
procedure MouseDoubleClic;
procedure MouseMiddleClic; overload;
procedure MouseMiddleClic(Tms: Cardinal; Shift: TShiftState); overload;
{ --- KeyBordState --- }
function KeyBoardState: TByteSet;
function CapsLocKEnable: Boolean;
{ --- Managed extended char by codename (cf unit KEYSDEF) --- }
function CodeKeyToAltNum(const Value: string): string;
{ --- routine "ProcessOnKey" param Value = ShortCut
sample : Value = 'Shift_Ctrl_a'
Value = 'Alt_F6'
Value = 'a e @ f alt_c suppr'
note : les touches de GAUCHE pour alt, ctrl, shift sont utilisées --- }
procedure ProcessOnKey(const Value: string);
{ --- Lire/écrire la dernière commande --- }
function RetrieveLastCommand: string;
procedure LastCommandToReg(const ASt: string);
type
TAllShortCut = TShortcuts;
var
AllShortCut : TAllShortCut;
const
WorkBufLen = 1024;
var
WorkBuf : array[0..WorkBufLen] of Char;
{Simulate key combinaison sample Alt+C to keyBoard
alt, shift, strl = left or right possible distinction
}
type
TKeyPressed = (kp_none, kp_shift, kp_ctrl, kp_alt, kp_alt_ctrl,
kp_shift_ctrl, kp_shift_alt);
TPushKeyCombine = class
private
FUpdated : Boolean;
FSHIFT_LEFT : Boolean;
FALT_LEFT : Boolean;
FCTRL_LEFT : Boolean;
FShift_pressed : Boolean;
FCtrl_pressed : Boolean;
FAlt_pressed : Boolean;
FResume : TKeyPressed;
private
procedure Initialize;
procedure Alt_Ctrl_Key(iKey : Smallint; iTime : Cardinal);
procedure Shift_Ctrl_Key(iKey: Smallint; iTime: Cardinal);
procedure Shift_alt_Key(iKey: Smallint; iTime: Cardinal);
procedure Alt_Key(iKey : Smallint; iTime : Cardinal);
procedure Ctrl_Key(iKey : Smallint; iTime : Cardinal);
procedure Shift_Key(iKey : Smallint; iTime : Cardinal);
procedure For_Key(iKey : Smallint; iTime : Cardinal);
procedure SetSpecials(const ASpecials: TSpecials);
private
function NospecialPressed:Boolean;
function ShiftPressed:Boolean;
function CtrlPressed:Boolean;
function AltPressed:Boolean;
function Alt_CtrlPressed:Boolean;
function Shift_CtrlPressed:Boolean;
function Shift_AltPressed: Boolean;
public
procedure KeyPress(Key: Char; Tms: Cardinal; Specials: TSpecials = []);
procedure KeyPressEx(iKey: Smallint; Tms: Cardinal; Specials: TSpecials = []);
constructor Create;
class procedure Simulate(Key: Char; Tms: Cardinal; Specials: TSpecials = []);
class procedure SimulateEx(iKey: Smallint; Tms: Cardinal; Specials: TSpecials = []);
end;
implementation
type
THKeys = array[0..pred(MaxLongInt)] of byte;
var
AllocationSize : integer;
(*
Envoi une chaîne de caractères à un composant windows disposant du focus
Example syntax: SendKeys('abc123def456ghi789', True);
*)
function SendKeys(SendKeysString : PWideChar; Wait : Boolean) : Boolean;
type
WBytes = array[0..pred(SizeOf(Word))] of Byte;
TSendKey = record
Name : ShortString;
VKey : Byte;
end;
const
{Array of keys that SendKeys recognizes.
If you add to this list, you must be sure to keep it sorted alphabetically
by Name because a binary search routine is used to scan it.}
MaxSendKeyRecs = 41;
SendKeyRecs : array[1..MaxSendKeyRecs] of TSendKey =
(
(Name: 'BACKSPACE'; VKey:VK_BACK),
(Name: 'BKSP'; VKey:VK_BACK),
(Name: 'BREAK'; VKey:VK_CANCEL),
(Name: 'BS'; VKey:VK_BACK),
(Name: 'CAPSLOCK'; VKey:VK_CAPITAL),
(Name: 'CLEAR'; VKey:VK_CLEAR),
(Name: 'DEL'; VKey:VK_DELETE),
(Name: 'DELETE'; VKey:VK_DELETE),
(Name: 'DOWN'; VKey:VK_DOWN),
(Name: 'END'; VKey:VK_END),
(Name: 'ENTER'; VKey:VK_RETURN),
(Name: 'ESC'; VKey:VK_ESCAPE),
(Name: 'ESCAPE'; VKey:VK_ESCAPE),
(Name: 'F1'; VKey:VK_F1),
(Name: 'F10'; VKey:VK_F10),
(Name: 'F11'; VKey:VK_F11),
(Name: 'F12'; VKey:VK_F12),
(Name: 'F13'; VKey:VK_F13),
(Name: 'F14'; VKey:VK_F14),
(Name: 'F15'; VKey:VK_F15),
(Name: 'F16'; VKey:VK_F16),
(Name: 'F2'; VKey:VK_F2),
(Name: 'F3'; VKey:VK_F3),
(Name: 'F4'; VKey:VK_F4),
(Name: 'F5'; VKey:VK_F5),
(Name: 'F6'; VKey:VK_F6),
(Name: 'F7'; VKey:VK_F7),
(Name: 'F8'; VKey:VK_F8),
(Name: 'F9'; VKey:VK_F9),
(Name: 'HELP'; VKey:VK_HELP),
(Name: 'HOME'; VKey:VK_HOME),
(Name: 'INS'; VKey:VK_INSERT),
(Name: 'LEFT'; VKey:VK_LEFT),
(Name: 'NUMLOCK'; VKey:VK_NUMLOCK),
(Name: 'PGDN'; VKey:VK_NEXT),
(Name: 'PGUP'; VKey:VK_PRIOR),
(Name: 'PRTSC'; VKey:VK_PRINT),
(Name: 'RIGHT'; VKey:VK_RIGHT),
(Name: 'SCROLLLOCK'; VKey:VK_SCROLL),
(Name: 'TAB'; VKey:VK_TAB),
(Name: 'UP'; VKey:VK_UP)
);
{Extra VK constants missing from Delphi's Windows API interface}
VK_NULL = 0;
VK_SemiColon = 186;
VK_Equal = 187;
VK_Comma = 188;
VK_Minus = 189;
VK_Period = 190;
VK_Slash = 191;
VK_BackQuote = 192;
VK_LeftBracket = 219;
VK_BackSlash = 220;
VK_RightBracket = 221;
VK_Quote = 222;
VK_Last = VK_Quote;
ExtendedVKeys : set of byte =
[VK_Up,
VK_Down,
VK_Left,
VK_Right,
VK_Home,
VK_End,
VK_Prior, {PgUp}
VK_Next, {PgDn}
VK_Insert,
VK_Delete];
const
INVALIDKEY = $FFFF {Unsigned -1};
VKKEYSCANSHIFTON = $01;
VKKEYSCANCTRLON = $02;
VKKEYSCANALTON = $04;
var
ShiftDown, ControlDown, AltDown : Boolean;
I, L : Integer;
MKey : Word;
function BitSet(BitTable, BitMask : Byte) : Boolean;
begin
Result := ByteBool(BitTable and BitMask);
end;
procedure SetBit(var BitTable : Byte; BitMask : Byte);
begin
BitTable := BitTable or Bitmask;
end;
procedure KeyboardEvent(VKey, ScanCode : Byte; Flags : Longint);
var
KeyboardMsg : TMsg;
begin
keybd_event(VKey, ScanCode, Flags,0);
if Wait then
while PeekMessage(KeyboardMsg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) do begin
TranslateMessage(KeyboardMsg);
DispatchMessage(KeyboardMsg)
end;
end;
procedure SendKeyDown(VKey: Byte; NumTimes : Word; GenUpMsg : Boolean);
var
Cnt : Word;
ScanCode : Byte;
NumState : Boolean;
KeyBoardState : TKeyboardState;
begin
if VKey = VK_NUMLOCK then begin
NumState := ByteBool(GetKeyState(VK_NUMLOCK) and 1);
GetKeyBoardState(KeyBoardState);
if NumState then KeyBoardState[VK_NUMLOCK] := (KeyBoardState[VK_NUMLOCK] and not 1)
else KeyBoardState[VK_NUMLOCK] := (KeyBoardState[VK_NUMLOCK] or 1);
SetKeyBoardState(KeyBoardState);
Exit
end;
ScanCode := Lo(MapVirtualKey(VKey, 0));
for Cnt := 1 to NumTimes do
if VKey in ExtendedVKeys then begin
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY);
if GenUpMsg then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
end else begin
KeyboardEvent(VKey, ScanCode, 0);
if GenUpMsg then KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
end
end;
procedure SendKeyUp(VKey: Byte);
var
ScanCode : Byte;
begin
ScanCode := Lo(MapVirtualKey(VKey, 0));
if VKey in ExtendedVKeys then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP)
else
KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP)
end;
procedure SendKey(MKey: Word; NumTimes : Word; GenDownMsg : Boolean);
begin
if BitSet(Hi(MKey), VKKEYSCANSHIFTON) then SendKeyDown(VK_SHIFT, 1, False);
if BitSet(Hi(MKey), VKKEYSCANCTRLON) then SendKeyDown(VK_CONTROL, 1, False);
if BitSet(Hi(MKey), VKKEYSCANALTON) then SendKeyDown(VK_MENU, 1, False);
SendKeyDown(Lo(MKey), NumTimes, GenDownMsg);
if BitSet(Hi(MKey), VKKEYSCANSHIFTON) then SendKeyUp(VK_SHIFT);
if BitSet(Hi(MKey), VKKEYSCANCTRLON) then SendKeyUp(VK_CONTROL);
if BitSet(Hi(MKey), VKKEYSCANALTON) then SendKeyUp(VK_MENU)
end;
procedure AltPlusKey(const Number: string);
var
i : Integer;
iKey : Integer;
begin
SendKeyDown(VK_MENU, 1, False);
try
SendKeyDown(VK_NUMPAD0, 1, False);
SendKeyUp(VK_NUMPAD0);
for I := 1 to Length(Number) do begin
iKey := VkKeyScan(Number[i]);
case IndexStr(Number[i], ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) of
0 : iKey := VK_NUMPAD0;
1 : iKey := VK_NUMPAD1;
2 : iKey := VK_NUMPAD2;
3 : iKey := VK_NUMPAD3;
4 : iKey := VK_NUMPAD4;
5 : iKey := VK_NUMPAD5;
6 : iKey := VK_NUMPAD6;
7 : iKey := VK_NUMPAD7;
8 : iKey := VK_NUMPAD8;
9 : iKey := VK_NUMPAD9;
end;
SendKeyDown(iKey, 1, False);
SendKeyUp(iKey)
end
finally
SendKeyUp(VK_MENU)
end;
end;
procedure SendKeyCirconflex(const car: Char);
begin
SendKeyDown(VkKeyScan('^'), 1, False);
SendKeyUp(VkKeyScan('^'));
SendKeyDown(VkKeyScan(car), 1, False);
SendKeyUp(VkKeyScan(car));
end;
procedure SendKeyTrema(const car: Char);
begin
SendKeyDown(VK_SHIFT, 1, False);
SendKeyDown(VkKeyScan('^'), 1, False);
SendKeyUp(VkKeyScan('^'));
SendKeyUp(VK_SHIFT);
SendKeyDown(VkKeyScan(car), 1, False);
SendKeyUp(VkKeyScan(car));
end;
{Implements a simple binary search to locate special key name strings}
function StringToVKey(KeyString : ShortString) : Word;
var
Found, Collided : Boolean;
Bottom, Top, Middle : Byte;
begin
Result := INVALIDKEY;
Bottom := 1;
Top := MaxSendKeyRecs;
Found := false;
Middle := (Bottom+Top) div 2;
repeat
Collided := (Bottom = Middle) or (Top = Middle);
if KeyString = SendKeyRecs[Middle].Name then begin
Found := True;
Result := SendKeyRecs[Middle].VKey;
end
else begin
if KeyString > SendKeyRecs[Middle].Name then Bottom := Middle
else Top := Middle;
Middle := Succ(Bottom+Top) div 2;
end;
until (Found or Collided);
if (Result = INVALIDKEY) then
MessageBox(0, 'Invalid key name!', 'SendKeys Routine', MB_ICONWARNING or MB_OK);
end;
procedure PopUpShiftKeys;
begin
if ShiftDown then SendKeyUp(VK_SHIFT);
if ControlDown then SendKeyUp(VK_CONTROL);
if AltDown then SendKeyUp(VK_MENU);
ShiftDown := false;
ControlDown := false;
AltDown := false;
end;
procedure PressKey(key: char);
begin
MKey := vkKeyScan(Key);
if (MKey <> INVALIDKEY) then begin
SendKey(MKey, 1, True);
PopUpShiftKeys;
end else
MessageBox(0, 'Invalid key name!', 'SendKeys Routine', MB_ICONWARNING or MB_OK);
end;
function PressFunction(index: Integer; Key: Byte):Integer;
begin
SendKeyDown(Key, 1, False);
Result := index + 1
end;
function PressKey_(index: Integer; Key: Char):Integer;
begin
PressKey(Key);
// if Key in ['^', '~'] then PressKey(' ');
if CharInSet(Key, ['^', '~']) then PressKey(' ');
Result := index + 1
end;
function FunctionKey(index: Integer):Integer;
begin
case SendKeysString[index] of
'a' : Result := PressFunction(index, VK_F1);
'b' : Result := PressFunction(index, VK_F2);
'c' : Result := PressFunction(index, VK_F3);
'd' : Result := PressFunction(index, VK_F4);
'e' : Result := PressFunction(index, VK_F5);
'f' : Result := PressFunction(index, VK_F6);
'g' : Result := PressFunction(index, VK_F7);
'h' : Result := PressFunction(index, VK_F8);
'i' : Result := PressFunction(index, VK_F9);
'j' : Result := PressFunction(index, VK_F10);
'k' : Result := PressFunction(index, VK_F11);
'l' : Result := PressFunction(index, VK_F12);
'+' : Result := PressKey_(index, '+');
'%' : Result := PressKey_(index, '%');
// '^' : Result := PressKey_(index, '^');
// '~' : Result := PressKey_(index, '~');
'"' : Result := PressKey_(index, '"');
else Result := 0
end;
end;
begin
AllocationSize := MaxInt;
Result := false;
ShiftDown := false;
ControlDown := false;
AltDown := false;
I := 0;
L := StrLen(SendKeysString);
if (L > AllocationSize) then L := AllocationSize;
if L = 0 then Exit;
while I < L do begin
case SendKeysString[I] of
'©' : begin //ALT
AltDown := True;
SendKeyDown(VK_MENU, 1, False);
Inc(I);
end;
'®' : begin //SHIFT
ShiftDown := True;
SendKeyDown(VK_SHIFT, 1, False);
Inc(I);
end;
'ª' : begin //CTRL
ControlDown := True;
SendKeyDown(VK_CONTROL, 1, False);
Inc(I);
end;
'¹' : begin //RETURN
SendKeyDown(VK_RETURN, 1, True);
PopUpShiftKeys;
Inc(I);
end;
'º' : begin
Inc(I);
// if (I < L) and (SendKeysString[I] in ['a'..'l','%','+','^','~','"'])
// then I := FunctionKey(I);
if (I < L) and charInSet(SendKeysString[I],['a'..'l','%','+','^','~','"'])
then I := FunctionKey(I);
end;
else begin
// By Huashan
// In double-byte character set systems, non ASCII characters cannot be
// sent by simulating keyboard stroke, we use IME messages
if Windows.IsDBCSLeadByte(Byte(SendKeysString[I])) then begin
try
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow, nil),
GetCurrentThreadId, True);
PostMessage(GetFocus, WM_IME_CHAR,
MakeWord(Byte(SendKeysString[I+1]), Byte(SendKeysString[I])), 0);
Inc(I);
finally
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow, nil),
GetCurrentThreadId, False);
end;
end else begin
MKey := vkKeyScan(SendKeysString[I]);
if (MKey <> INVALIDKEY) then begin
SendKey(MKey, 1, True);
PopUpShiftKeys;
end else begin
case IndexStr(SendKeysString[I], ['ô','î', 'â', 'ï',
'Â', 'À', 'É', 'È', 'Ê', 'ë', 'Ë', 'ê', 'Ô', 'Ï', 'Î', 'Ç', 'ç']) of
0 : SendKeyCirconflex('o');
1 : SendKeyCirconflex('i');
2 : SendKeyCirconflex('a');
3 : SendKeyTrema('i');
4 : SendKeyCirconflex('A');
5 : AltPlusKey('192');
6 : AltPlusKey('201');
7 : AltPlusKey('200');
8 : AltPlusKey('202');
9 : AltPlusKey('235');
10 : AltPlusKey('203');
11 : AltPlusKey('234');
12 : AltPlusKey('212');
13 : AltPlusKey('207');
14 : AltPlusKey('206');
15 : AltPlusKey('199');
16 : AltPlusKey('231');
-1 : MessageBox(0, 'Invalid key name!', 'SendKeys Routine', MB_ICONWARNING or MB_OK);
end;
// Inc(I);
end;
end;
Inc(I);
end;
end;
end;
Result := true;
PopUpShiftKeys;
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SIMULATION DE L'APPUI DE TOUCHES WINDOWS + CHAR AVEC TEMPS DATTENTE
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
procedure SendKeyWin(iKey: Smallint; Tms: Cardinal);
begin
keybd_event(VK_LWIN, MapVirtualKey(VK_LWIN, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), 0, 0);
WaitForKey( Tms );
keybd_event(iKey, MapVirtualKey(iKey, 0), KEYEVENTF_KEYUP, 0);
keybd_event(VK_LWIN, MapVirtualKey(VK_LWIN, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0)
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SIMULATION DE L'APPUI DE TOUCHES AVEC TEMPS DATTENTE ET
LA DISTINCTION ENTRE LES TOUCHES ALT,SHIFT,CRTL - GEUCHE ET DROITE
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
procedure SendKey(iKey: Smallint; Tms: Cardinal; Specials: TSpecials = []);
begin
TPushKeyCombine.SimulateEx(iKey, Tms, Specials);
end;
const
ExtendedVKeys : set of byte =
[VK_Up,
VK_Down,
VK_Left,
VK_Right,
VK_Home,
VK_End,
VK_Prior, {PgUp}
VK_Next, {PgDn}
VK_Insert,
VK_Delete
];
procedure Delay(ms: Cardinal);
var
S : Cardinal;
C : Cardinal;
begin
C := 0;
S := GetTickCount + ms;
with Application do
repeat
Sleep( 10 );
C := C + 10;
if C mod 90 = 0 then Application.ProcessMessages;
until Application.Terminated or (GetTickCount > S)
end;
procedure Wait(Time: Cardinal);
begin
Delay( Time );
end;
procedure WaitForKey(Time: Cardinal);
begin
Delay( Time );
end;
{ TPushKeyCombine }
function TPushKeyCombine.AltPressed: Boolean;
begin
Result := not FShift_pressed and not FCtrl_pressed and FAlt_pressed
end;
function TPushKeyCombine.Alt_CtrlPressed: Boolean;
begin
Result := not FShift_pressed and FCtrl_pressed and FAlt_pressed
end;
procedure TPushKeyCombine.Alt_Ctrl_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
if FCTRL_LEFT
then keybd_event(VK_LCONTROL, MapVirtualKey(VK_LCONTROL, 0), 0, 0)
else keybd_event(VK_RCONTROL, MapVirtualKey(VK_RCONTROL, 0), KEYEVENTF_EXTENDEDKEY, 0);
if FALT_LEFT
then keybd_event(VK_LMENU, MapVirtualKey(VK_LMENU, 0), 0, 0)
else keybd_event(VK_RMENU, MapVirtualKey(VK_RMENU, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
if FALT_LEFT
then keybd_event(VK_LMENU, MapVirtualKey(VK_LMENU, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RMENU, MapVirtualKey(VK_RMENU, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
if FCTRL_LEFT
then keybd_event(VK_LCONTROL, MapVirtualKey(VK_LCONTROL, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RCONTROL, MapVirtualKey(VK_RCONTROL, 0),KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
procedure TPushKeyCombine.Shift_Ctrl_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
if FCTRL_LEFT
then keybd_event(VK_LCONTROL, MapVirtualKey(VK_LCONTROL, 0), 0, 0)
else keybd_event(VK_RCONTROL, MapVirtualKey(VK_RCONTROL, 0), KEYEVENTF_EXTENDEDKEY, 0);
if FALT_LEFT
then keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), 0, 0)
else keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
if FALT_LEFT
then keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
if FCTRL_LEFT
then keybd_event(VK_LCONTROL, MapVirtualKey(VK_LCONTROL, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RCONTROL, MapVirtualKey(VK_RCONTROL, 0),KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
procedure TPushKeyCombine.Shift_alt_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
if FCTRL_LEFT
then keybd_event(VK_LMENU, MapVirtualKey(VK_LMENU, 0), 0, 0)
else keybd_event(VK_RMENU, MapVirtualKey(VK_RMENU, 0), KEYEVENTF_EXTENDEDKEY, 0);
if FALT_LEFT
then keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), 0, 0)
else keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
if FALT_LEFT
then keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
if FCTRL_LEFT
then keybd_event(VK_LMENU, MapVirtualKey(VK_LMENU, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RMENU, MapVirtualKey(VK_RMENU, 0),KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
procedure TPushKeyCombine.Alt_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
if FALT_LEFT
then keybd_event(VK_LMENU, MapVirtualKey(VK_LMENU, 0), 0, 0)
else keybd_event(VK_RMENU, MapVirtualKey(VK_RMENU, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
if FALT_LEFT
then keybd_event(VK_LMENU, MapVirtualKey(VK_LMENU, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RMENU, MapVirtualKey(VK_RMENU, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
constructor TPushKeyCombine.Create;
begin
inherited Create;
Initialize;
end;
function TPushKeyCombine.CtrlPressed: Boolean;
begin
Result := not FShift_pressed and FCtrl_pressed and not FAlt_pressed
end;
procedure TPushKeyCombine.Ctrl_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
if FCTRL_LEFT
then keybd_event(VK_LCONTROL, MapVirtualKey(VK_LCONTROL, 0), 0, 0)
else keybd_event(VK_RCONTROL, MapVirtualKey(VK_RCONTROL, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
if FCTRL_LEFT
then keybd_event(VK_LCONTROL, MapVirtualKey(VK_LCONTROL, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RCONTROL, MapVirtualKey(VK_RCONTROL, 0), KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
procedure TPushKeyCombine.For_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
procedure TPushKeyCombine.Initialize;
begin
FUpdated := False;
FSHIFT_LEFT := True;
FALT_LEFT := True;
FCTRL_LEFT := True;
FShift_pressed := False;
FCtrl_pressed := False;
FAlt_pressed := False;
FResume := kp_none;
end;
procedure TPushKeyCombine.KeyPress(Key: Char; Tms: Cardinal;
Specials: TSpecials);
begin
KeyPressEx( VkKeyScan(Key), Tms, Specials )
end;
procedure TPushKeyCombine.KeyPressEx(iKey: Smallint; Tms: Cardinal;
Specials: TSpecials);
begin
SetSpecials( Specials );
case FResume of
kp_none : For_Key ( iKey, Tms );
kp_shift : Shift_Key ( iKey, Tms );
kp_ctrl : Ctrl_Key ( iKey, Tms );
kp_alt : Alt_Key ( iKey, Tms );
kp_alt_ctrl : Alt_Ctrl_Key ( iKey, Tms );
kp_shift_ctrl : Shift_Ctrl_Key ( iKey, Tms );
kp_shift_alt : Shift_alt_Key ( iKey, Tms );
end;
end;
function TPushKeyCombine.NospecialPressed: Boolean;
begin
Result := not FShift_pressed and not FCtrl_pressed and not FAlt_pressed
end;
procedure TPushKeyCombine.SetSpecials(const ASpecials: TSpecials);
begin
if ss_rshift in ASpecials then begin FSHIFT_LEFT := False; FShift_pressed := True end;
if ss_lshift in ASpecials then begin FSHIFT_LEFT := True; FShift_pressed := True end;
if ss_rctrl in ASpecials then begin FCTRL_LEFT := False; FCtrl_pressed := True end;
if ss_lctrl in ASpecials then begin FCTRL_LEFT := True; FCtrl_pressed := True end;
if ss_ralt in ASpecials then begin FALT_LEFT := False; FAlt_pressed := True end;
if ss_lalt in ASpecials then begin FALT_LEFT := True; FAlt_pressed := True end;
if NospecialPressed then FResume := kp_none;
if ShiftPressed then FResume := kp_shift;
if CtrlPressed then FResume := kp_ctrl;
if AltPressed then FResume := kp_alt;
if Alt_CtrlPressed then FResume := kp_alt_ctrl;
if Shift_CtrlPressed then FResume := kp_shift_ctrl;
if Shift_AltPressed then FResume := kp_shift_alt;
end;
function TPushKeyCombine.ShiftPressed: Boolean;
begin
Result := FShift_pressed and not FCtrl_pressed and not FAlt_pressed
end;
function TPushKeyCombine.Shift_CtrlPressed : Boolean;
begin
Result := FShift_pressed and FCtrl_pressed and not FAlt_pressed
end;
function TPushKeyCombine.Shift_AltPressed: Boolean;
begin
Result := FShift_pressed and not FCtrl_pressed and FAlt_pressed
end;
procedure TPushKeyCombine.Shift_Key(iKey: Smallint; iTime: Cardinal);
var
Flag: Cardinal;
begin
if not FUpdated then
try
FUpDated := True;
Flag := 0;
if (iKey in ExtendedVKeys) then Flag := KEYEVENTF_EXTENDEDKEY;
if FSHIFT_LEFT
then keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), 0, 0)
else keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), 0, 0);
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag, 0);
WaitForKey( iTime );
keybd_event(iKey, MapVirtualKey(iKey, 0), Flag or KEYEVENTF_KEYUP, 0);
if FSHIFT_LEFT
then keybd_event(VK_LSHIFT, MapVirtualKey(VK_LSHIFT, 0), KEYEVENTF_KEYUP, 0)
else keybd_event(VK_RSHIFT, MapVirtualKey(VK_RSHIFT, 0), KEYEVENTF_KEYUP, 0);
finally
FUpdated := False;
end;
end;
class procedure TPushKeyCombine.Simulate(Key: Char; Tms: Cardinal;
Specials: TSpecials);
begin
with TPushKeyCombine.Create do
try
KeyPress(Key, Tms, Specials)
finally
Free
end
end;
class procedure TPushKeyCombine.SimulateEx(iKey: Smallint; Tms: Cardinal;
Specials: TSpecials);
begin
with TPushKeyCombine.Create do
try
KeyPressEx(iKey, Tms, Specials)
finally
Free
end
end;
procedure SendKey(sCode: string); overload;
var
i : Integer;
x : Smallint;
begin
try
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
for i := 1 to Length(sCode) do begin
case sCode[i] of
'0' : x := VK_NUMPAD0;
'1' : x := VK_NUMPAD1;
'2' : x := VK_NUMPAD2;
'3' : x := VK_NUMPAD3;
'4' : x := VK_NUMPAD4;
'5' : x := VK_NUMPAD5;
'6' : x := VK_NUMPAD6;
'7' : x := VK_NUMPAD7;
'8' : x := VK_NUMPAD8;
'9' : x := VK_NUMPAD9;
else x := VkKeyScan(sCode[i])
end;
keybd_event(x, MapVirtualKey(x, 0), 0, 0);
keybd_event(x, MapVirtualKey(x, 0), KEYEVENTF_KEYUP, 0);
end;
finally
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0)
end
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
FOR MOUSE CLICK
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
procedure MouseLeftClic;
begin
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
end;
procedure MouseRightClic;
begin
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
end;
procedure MouseMiddleClic;
begin
mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
end;
procedure MouseDoubleClic;
begin
MouseLeftClic;
MouseLeftClic;
end;
procedure MouseLeft_(Tms: Cardinal);
begin
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
WaitForKey( Tms );
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
end;
procedure MouseLeft_Shift_(Tms: Cardinal);
begin
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
WaitForKey( Tms );
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0)