一个处理多键盘输入的组件
最近的项目中需要同时处理来自多个键盘的输入信息,使用raw input可以完美的解决这个问题。封装成一个组件,代码如下:
运行效果:
1
unit UnitRawInput;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
interface
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
uses
6
SysUtils, Classes,Windows,uRawInput,Forms,messages;
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
type
10
TRawKeyPressEvent = procedure(sender:TObject;key:word;KeyboardHandle:THANDLE) of object;
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
TRawKeyDownEvent = TRawKeyPressEvent;
13
TRawKeyUpEvent = TRawKeyPressEvent;
14
type
15
TRawInputKeyboard = class(TComponent)
16
private
17
FOldOnMessage:TMessageEvent;
18
FOnRawKeyDown:TRawKeyPressEvent;
19
FOnRawKeyUp:TRawKeyPressEvent;
20
procedure ApplicationEventsMessage(var Msg: tagMSG; var Handled: Boolean);
21
procedure RegisterRaw;
22
protected
23
procedure ProcessKeyPressMessage(var Msg: tagMSG; Keyboard:RAWKEYBOARD;KeyboardHandle:THANDLE);
24
procedure DoRawKeyDown(Key: Word;KeyboardHandle:THANDLE);
25
procedure DoRawKeyUp(Key: Word;KeyboardHandle:THANDLE);
26
public
27
constructor Create(AOwner: TComponent); override;
28
destructor Destroy; override;
29
published
30
property OnRawKeyDown:TRawKeyDownEvent read FOnRawKeyDown write FOnRawKeyDown;
31
property OnRawKeyUp:TRawKeyUpEvent read FOnRawKeyUp write FOnRawKeyUp;
32
end;
33![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
procedure Register;
35![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
implementation
37![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
procedure Register;
39
begin
40
RegisterComponents('lance', [TRawInputKeyboard]);
41
end;
42![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
{ TRawInput }
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
procedure TRawInputKeyboard.ApplicationEventsMessage(var Msg: tagMSG;
46
var Handled: Boolean);
47
var
48
Size: cardinal;
49
ttagRAWINPUT: tagRAWINPUT;
50![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
ttagRAWKEYBOARD:tagRAWKEYBOARD;
52
KeyboardHandle:THANDLE;
53![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
begin
56
if (msg.message=WM_INPUT) then
57
begin
58
Size:= sizeOf(RAWINPUTHEADER);
59
ttagRAWINPUT.header.dwSize := sizeOf(RAWINPUTHEADER);
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
if GetRawInputData(HRAWINPUT(Msg.LParam),
62
RID_HEADER, @ttagRAWINPUT, Size, sizeof(RAWINPUTHEADER))>0 then
63
begin
64
if (ttagRAWINPUT.header.dwType = RIM_TYPEKEYBOARD) then
65
begin
66
Size := ttagRAWINPUT.header.dwSize;
67
if GetRawInputData (HRAWINPUT(Msg.LParam),
68
RID_INPUT, @ttagRAWINPUT, Size, sizeOf(RAWINPUTHEADER))>= 0 then
69
begin
70
ttagRAWKEYBOARD.VKey := ttagRAWINPUT.keyboard.VKey;
71
ttagRAWKEYBOARD.Flags := ttagRAWINPUT.keyboard.Flags;
72
ttagRAWKEYBOARD.MakeCode := ttagRAWINPUT.keyboard.MakeCode;
73
ttagRAWKEYBOARD.Message := ttagRAWINPUT.keyboard.Message;
74
KeyboardHandle := ttagRAWINPUT.header.hDevice;
75
ProcessKeyPressMessage(msg,ttagRAWKEYBOARD,KeyboardHandle);
76
end;
77
end;
78
end;
79![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
80
end; //end if (msg.message=WM_INPUT) then
81![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
82
if assigned(FOldOnMessage) then FOldOnMessage( Msg,Handled);
83
end;
84![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
86
constructor TRawInputKeyboard.Create(AOwner: TComponent);
87
begin
88
inherited Create(AOwner);;
89
FOldOnMessage:=Application.OnMessage;
90
Application.OnMessage:=ApplicationEventsMessage;
91
RegisterRaw;
92
end;
93![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
94
destructor TRawInputKeyboard.Destroy;
95
begin
96
inherited;
97
end;
98
procedure TRawInputKeyboard.ProcessKeyPressMessage(var Msg: tagMSG;
99
Keyboard: RAWKEYBOARD; KeyboardHandle: THANDLE);
100
begin
101
case Keyboard.Message of
102
WM_KEYDOWN : DoRawKeyDown(keyboard.VKey,KeyboardHandle);
103
WM_KEYUP : DoRawKeyUp(Keyboard.VKey,KeyboardHandle);
104
end;
105
end;
106
procedure TRawInputKeyboard.DoRawKeyDown(Key: Word;KeyboardHandle: THANDLE);
107
begin
108
if assigned(FOnRawKeyDown) then
109
FOnRawKeyDown(self,key,KeyboardHandle);
110
end;
111
procedure TRawInputKeyboard.DoRawKeyUp(Key: Word;KeyboardHandle: THANDLE);
112
begin
113
if assigned(FOnRawKeyUp) then
114
FOnRawKeyUp(self,key,KeyboardHandle);
115
end;
116
procedure TRawInputKeyboard.RegisterRaw;
117
var
118
Size: Cardinal;
119
RRawIinputDevice: array[0..0] of RAWINPUTDEVICE;
120
begin
121
RRawIinputDevice[0].usUsagePage := 1;
122
RRawIinputDevice[0].usUsage := 6;
123
RRawIinputDevice[0].dwFlags := RIDEV_CAPTUREMOUSE;
124
RRawIinputDevice[0].hwndTarget := Application.Handle;
125
Size:= sizeOf(RAWINPUTDEVICE);
126
if not (RegisterRawInputDevices(@RRawIinputDevice, 1, Size)) then
127
begin
128
raise Exception.Create('RegisterRawInputDevices error!');
129
end
130
end;
131![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
132
end.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
108
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
109
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
110
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
111
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
122
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
128
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
129
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
130
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
131
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
132
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行效果: