-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuInputUtils.pas
91 lines (71 loc) · 2.66 KB
/
uInputUtils.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
unit uInputUtils;
interface
uses
Windows;
type
TKeyState = (ksDown, ksUp);
function SendInputKey(AVirtualKey: Integer; AScanCode: Integer;
AKeyState: TKeyState): Boolean;
implementation
{
https://stackoverflow.com/a/12565963/22837574
"Disclaimer:
I'm not answering the question as it is. I'm trying to propose a way I'll
rather follow when I'd need a virtual keyboard.
1. What about ready made component ?
I would suggest you to use the TTouchKeyboard component, which is a VCL
component, representing a virtual keyboard. That said, you're developing
something, what is already a part of Delphi distributions. It's a part of
Delphi since version 2010, but I can't say in which distributions.
2. It looks ugly and I'll rather make my own:
When I've seen TTouchKeyboard component for the first time, I was hoping
that allows owner drawing. Well, unfortunately doesn't. In that case I'd
try to simulate key strokes by myself rather than solve cases like this
for another components you may soon or later use.
2.1. How to simulate keystrokes in your own way ?
The following code simulates keystrokes by using the SendInput function
and it's based on the code used by TTouchKeyboard component:
(* SendInputKey function *)
And the usage of the above function. You can pass virtual key, scan code
or both to this function. When you'll be unsure with either of them, pass
value of -1 and the key code will be additionally mapped by the MapVirtualKey
function. The following example shows how to send a Backspace and then
Shift + A:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
SendInputKey(VK_BACK, -1, ksDown);
SendInputKey(VK_BACK, -1, ksUp);
SendInputKey(VK_SHIFT, -1, ksDown);
SendInputKey(Ord('A'), -1, ksDown);
SendInputKey(Ord('A'), -1, ksUp);
SendInputKey(VK_SHIFT, -1, ksUp);
end;
}
function SendInputKey(AVirtualKey: Integer; AScanCode: Integer;
AKeyState: TKeyState): Boolean;
var
Input: TInput;
begin
Input.Itype := INPUT_KEYBOARD;
if (AVirtualKey = -1) and (AScanCode >= 0) then
begin
Input.ki.wVk := MapVirtualKey(AScanCode, MAPVK_VSC_TO_VK);
Input.ki.wScan := AScanCode;
end
else if (AVirtualKey >= 0) and (AScanCode = -1) then
begin
Input.ki.wVk := AVirtualKey;
Input.ki.wScan := MapVirtualKey(AVirtualKey, MAPVK_VK_TO_VSC);
end
else if (AVirtualKey >= 0) and (AScanCode >= 0) then
begin
Input.ki.wVk := AVirtualKey;
Input.ki.wScan := AScanCode;
end;
case AKeyState of
ksDown: Input.ki.dwFlags := 0;
ksUp: Input.ki.dwFlags := KEYEVENTF_KEYUP;
end;
Result := SendInput(1, Input, SizeOf(TInput)) = 1;
end;
end.