Debian Codegraph @

src/ui/castleinternalpk3dconnexion.pas

raw
1 {*------------------------------------------------------------------------------
2 Receiving input from a 3DConnexion device, e.g. a SpacePilot.
3
4 @Author Patrick M. Kolla
5 @Version 0.2.0
6 -------------------------------------------------------------------------------}
7 // *****************************************************************************
8 // Copyright: © 2007 Patrick M. Kolla. All rights reserved.
9 // File: pk3DConnexion.pas
10 // License: LGPL 2.1
11 // Compiler: Delphi 2006
12 // Purpose: Receiving input from a 3DConnexion device, e.g. a SpacePilot.
13 // Authors: Patrick M. Kolla (pk)
14 // *****************************************************************************
15 // 3DConnexion, SpacePilot, and probably other terms as well are trademarks
16 // of 3DConnextion, A LogiTech company, all rights to these names are with
17 // them, and the names are only used to identify the use of this unit, which
18 // has been created without any commercial aim.
19 // *****************************************************************************
20 // This library is free software; you can redistribute it and/or
21 // modify it under the terms of the GNU Lesser General Public
22 // License as published by the Free Software Foundation; either
23 // version 2.1 of the License, or (at your option) any later version.
24 //
25 // This library is distributed in the hope that it will be useful,
26 // but WITHOUT ANY WARRANTY; without even the implied warranty of
27 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 // Lesser General Public License for more details.
29 //
30 // You should have received a copy of the GNU Lesser General Public
31 // License along with this library; if not, write to the Free Software
32 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 // *****************************************************************************
34 // Dependencies:
35 // TDxInput_TLB - Type library unit for 3DConnexion COM driver
36 // *****************************************************************************
37 // Changelog (new entries first):
38 // ---------------------------------------
39 // 2007-06-02 pk RLS 0.2.0
40 // 2007-06-02 pk 1h Improved custom emulation settings
41 // 2007-05-31 pk RLS 0.1.0
42 // 2007-05-31 pk 15m Added header and some comments
43 // 2007-05-31 pk 30m Added and tested mouse emulation
44 // 2007-05-30 pk 2h Experimenting with COM objects
45 // *****************************************************************************
46
47 unit CastleInternalPk3DConnexion;
48
49 { TODO -oPatrick : React to newly connected devices! }
50
51 {$I castleconf.inc}
52
53 {$ifdef MSWINDOWS}
54 {$ifndef CASTLE_DELPHI_PACKAGE}
55 {$define REAL_3D_CONNEXION_IMPLEMENTATION}
56 {$endif}
57 {$endif}
58
59 interface
60
61 {$ifdef REAL_3D_CONNEXION_IMPLEMENTATION}
62 uses
63 SysUtils,
64 Windows,
65 Classes,
66 ActiveX,
67 CastleInternalTDxInput_TLB;
68 {$endif}
69
70 type
71 TEmulationAxis = (eaRotateX, eaRotateY, eaRotateZ, eaRotateAngle,
72 eaTranslateX, eaTranslateY, eaTranslateZ, eaTranslateLength);
73 TEmulationDetail = (edNone, edMouseX, edMouseY, edMouseWheelV, edMouseWheelH,
74 edKeyboardArrowsLR, edKeyboardArrowsUD, edKeyboardPageUD,
75 edKeyboardCustom);
76 TEmulationType = (etNone, etMouse, etKeyboard, etMouseKeyboard, etCustom);
77 TEmulationAxisSetting = record
78 Detail: TEmulationDetail;
79 KeyLeft, KeyRight: byte;
80 Threshold, Multiplier: integer;
81 end;
82 TEmulationSettings = array[TEmulationAxis] of TEmulationAxisSetting;
83
84 const
85 SEmulationAxis : array[TEmulationAxis] of string =
86 ('Rotate (X)', 'Rotate (Y)', 'Rotate (Z)', 'Rotate (Angle)',
87 'Translate (X)', 'Translate (Y)', 'Translate (Z)', 'Translate (Length)');
88 SEmulationAxisIDs : array[TEmulationAxis] of string =
89 ('RotateX', 'RotateY', 'RotateZ', 'RotateAngle',
90 'TranslateX', 'TranslateY', 'TranslateZ', 'TranslateLength');
91 SEmulationType : array[TEmulationType] of string =
92 ('None', 'Mouse', 'Keyboard', 'Mouse & Keyboard', 'Custom');
93 SEmulationDetail : array[TEmulationDetail] of string =
94 ('None', 'Mouse: X-axis', 'Mouse: Y-axis',
95 'Mouse: Wheel (vertical)', 'Mouse: Wheel (horizontal)',
96 'Keyboard: Arrows left/right', 'Keyboard: Arrows up/down',
97 'Keyboard: Page up/down', 'Keyboard: Custom');
98 SVirtualKeys : array[0..$AF] of string =
99 ('No key assigned!',
100 'Left mouse button (VK_LBUTTON)',
101 'Right mouse button (VK_RIGHT_BUTTON)',
102 'Cancel (VK_CANCEL)',
103 'Middle mouse button (VK_MBUTTON)',
104 'X1 mouse button (VK_XBUTTON1)',
105 'X2 mouse button (VK_XBUTTON2)',
106 ' --- Undefined',
107 'Backspace (VK_BACK)',
108 'Tabulator (VK_TAB)',
109 ' --- Reserved', // 0A
110 ' --- Reserved', // 0B
111 'Clear (VK_CLEAR)',
112 'Return/Enter (VK_RETURN)',
113 ' --- Undefined', // 0E
114 ' --- Undefined', // OF
115 'Shift (VK_SHIFT)',
116 'Control (VK_CONTROL)',
117 'Alt/Menu (VK_ALT)',
118 'Pause (VK_PAUSE)',
119 'Caps Lock (VK_CAPITAL)',
120 'IME: Kana/Hanguel/Hangul mode (VK_KANA/VK_HANGUEL/VK_HANGUL)',
121 ' --- Undefined', // 16
122 'IME: Junja mode (VK_JUNJA)',
123 'IME: Final mode (VK_FINAL)',
124 'IME: Hanja/Kanji mode (VK_HANJA/VK_KANJI)',
125 ' --- Undefined', // 1A
126 'Escape (VK_ESCAPE)',
127 'IME: convert (VK_CONVERT)',
128 'IME: nonconvert (VK_NONCONVERT)',
129 'IME: accept (VK_ACCEPT)',
130 'IME: mode change request (VK_MODECHANGE', // 1F
131 'Spacebar (VK_SPACE)',
132 'Page up (VK_PRIOR)',
133 'Page down (VK_NEXT)',
134 'End (VK_END)',
135 'Home (VK_HOME)',
136 'Arrow left (VK_LEFT)',
137 'Arrow up (VK_UP)',
138 'Arrow right (VK_RIGHT)',
139 'Arrow down (VK_DOWN)',
140 'Select (VK_SELECT)',
141 'Print (VK_PRINT)',
142 'Execute (VK_EXECUTE)',
143 'Print Screen (VK_SNAPSHOT)',
144 'Insert (VK_INSERT)',
145 'Delete (VK_DELETE)',
146 'Help (VK_HELP)',
147 '0 (number key)',
148 '1 (number key)',
149 '2 (number key)',
150 '3 (number key)',
151 '4 (number key)',
152 '5 (number key)',
153 '6 (number key)',
154 '7 (number key)',
155 '8 (number key)',
156 '9 (number key)',
157 ' --- Undefined', // 3A
158 ' --- Undefined', // 3B
159 ' --- Undefined', // 3C
160 ' --- Undefined', // 3D
161 ' --- Undefined', // 3E
162 ' --- Undefined', // 3F
163 ' --- Undefined', // 40
164 'A (letter key)',
165 'B (letter key)',
166 'C (letter key)',
167 'D (letter key)',
168 'E (letter key)',
169 'F (letter key)',
170 'G (letter key)',
171 'H (letter key)',
172 'I (letter key)',
173 'J (letter key)',
174 'K (letter key)',
175 'L (letter key)',
176 'M (letter key)',
177 'N (letter key)',
178 'O (letter key)',
179 'P (letter key)',
180 'Q (letter key)',
181 'R (letter key)',
182 'S (letter key)',
183 'T (letter key)',
184 'U (letter key)',
185 'V (letter key)',
186 'W (letter key)',
187 'X (letter key)',
188 'Y (letter key)',
189 'Z (letter key)',
190 'Left Windows key (VK_LWIN)',
191 'Right Windows key (VK_RWIN)',
192 'Applications key (VK_APPS)',
193 ' --- Reserved', // 5E
194 'Sleep (VK_SLEEP)',
195 '0 (VK_NUMPAD0)',
196 '1 (VK_NUMPAD1)',
197 '2 (VK_NUMPAD2)',
198 '3 (VK_NUMPAD3)',
199 '4 (VK_NUMPAD4)',
200 '5 (VK_NUMPAD5)',
201 '6 (VK_NUMPAD6)',
202 '7 (VK_NUMPAD7)',
203 '8 (VK_NUMPAD8)',
204 '9 (VK_NUMPAD9)',
205 'Multiply (VK_MULTIPLY)',
206 'Add (VK_ADD)',
207 'Separator (VK_SEPARATOR)',
208 'Subtract (VK_SUBTRACT)',
209 'Decimal (VK_DECIMAL)',
210 'Divide (VK_DIVIDE)',
211 'F1 (VK_F1)',
212 'F2 (VK_F2)',
213 'F3 (VK_F3)',
214 'F4 (VK_F4)',
215 'F5 (VK_F5)',
216 'F6 (VK_F6)',
217 'F7 (VK_F7)',
218 'F8 (VK_F8)',
219 'F9 (VK_F9)',
220 'F10 (VK_F10)',
221 'F11 (VK_F11)',
222 'F12 (VK_F12)',
223 'F13 (VK_F13)',
224 'F14 (VK_F14)',
225 'F15 (VK_F15)',
226 'F16 (VK_F16)',
227 'F17 (VK_F17)',
228 'F18 (VK_F18)',
229 'F19 (VK_F19)',
230 'F20 (VK_F20)',
231 'F21 (VK_F21)',
232 'F22 (VK_F22)',
233 'F23 (VK_F23)',
234 'F24 (VK_F24)',
235 ' --- Undefined', // 88
236 ' --- Undefined', // 89
237 ' --- Undefined', // 8A
238 ' --- Undefined', // 8B
239 ' --- Undefined', // 8C
240 ' --- Undefined', // 8D
241 ' --- Undefined', // 8E
242 ' --- Undefined', // 8F
243 'Num lock (VK_NUMLOCK)',
244 'Scroll lock (VK_SCROLL)',
245 ' --- OEM specific', // 92
246 ' --- OEM specific', // 93
247 ' --- OEM specific', // 94
248 ' --- OEM specific', // 95
249 ' --- OEM specific', // 96
250 ' --- Undefined', // 97
251 ' --- Undefined', // 98
252 ' --- Undefined', // 99
253 ' --- Undefined', // 9A
254 ' --- Undefined', // 9B
255 ' --- Undefined', // 9C
256 ' --- Undefined', // 9D
257 ' --- Undefined', // 9E
258 ' --- Undefined', // 9F
259 'Left shift key (VK_LSHIFT)',
260 'Right shift key (VK_RSHIFT)',
261 'Left control key (VK_LCONTROL)',
262 'Right control key (VK_RCONTROL)',
263 'Left alt/menu key (VK_LMENU)',
264 'Right alt/menu key (VK_RMENU)',
265 'Browser back (VK_BROWSER_BACK)', // A6
266 'Browser forward (VK_BROWSER_FORWARD)',
267 'Browser refresh (VK_BROWSER_REFRESH)',
268 'Browser stop (VK_BROWSER_STOP)',
269 'Browser search (VK_BROWSER_SEARCH)',
270 'Browser favorites (VK_BROWSER_FAVORITES)',
271 'Browser home (VK_BROWSER_HOME)',
272 'Volume mute (VK_VOLUME_MUTE)',
273 'Volume down (VK_VOLUME_DOWN)',
274 'Volume up (VK_VOLUME_UP)'
275 );
276
277
278 type
279 // Rotation on X,Y,Z is -1..1, Angle is 0..N
280 // N depends on speed setting, up to 1600
281 TOnRotation = procedure(const X, Y, Z, Angle: Double) of object;
282 // Translation on X,Y,Z is -N..N, Length is 0..N
283 // N depends on speed setting, up to 1600
284 TOnTranslation = procedure(const X, Y, Z, Length: Double) of object;
285 TOnKeyChange = procedure(const KeyIndex: integer; const Pressed: boolean) of object;
286 TOnKeySomething = procedure(const KeyIndex: integer) of object;
287
288 { Surround most of the content in Windows-only ifdef.
289 Still, the above types, like TEmulationAxis, are defined on all OSes.
290 This is useful e.g. to detect name clash errors on any OS. }
291 {$ifdef REAL_3D_CONNEXION_IMPLEMENTATION}
292
293 type
294 T3DConnexionDevice = class(TObject)
295 private
296 FDevice: ISimpleDevice; /// Device interface
297 FKeyboard: IKeyboard; /// Keyboard interface
298 FLastResult: HResult; /// Error code for last COM action
299 FLoaded: boolean; /// Whether connection to COM object was successful
300 FRevisionNumber: widestring; /// COM driver revision? (not the same as driver version, e.g. 1.1.0 vs. 3.3.6)
301 FSensor: ISensor; /// Sensor interface
302 FKeyStates: array of boolean; /// Internal memory for last key stati
303 FOnRotation: TOnRotation; /// Event for rotations
304 FOnTranslation: TOnTranslation; /// Event for translations
305 FOnKeyChange: TOnKeyChange; /// Event for key stati
306 FOnKeyDown: TOnKeySomething; /// Event for key down
307 FOnKeyUp: TOnKeySomething; /// Event for key up
308 FKeyboardEmulationThreshold: integer; /// Only fire keys if amplitude larger than this
309 FEmulationType: TEmulationType;
310 FCustomEmulations: TEmulationSettings;
311 FEmulatedKeyPressedStates: array[TEmulationAxis, false..true] of boolean;
312 procedure HandleCustomDetail(const Axis: TEmulationAxis; const Value: Double);
313 procedure FireKeyChange(const KeyIndex: integer; const Pressed: boolean);
314 procedure FireRotation(const X, Y, Z, Angle: Double); overload;
315 procedure FireRotation(const Rotation: IAngleAxis); overload;
316 procedure FireTranslation(const X, Y, Z, Length: Double); overload;
317 procedure FireTranslation(const Translation: IVector3D); overload;
318 function GetSensorPeriod: Double;
319 function GetCustomEmulations(AIndex: TEmulationAxis): TEmulationAxisSetting;
320 procedure SetCustomEmulations(AIndex: TEmulationAxis; const Value: TEmulationAxisSetting);
321 protected
322 public
323 constructor Create(const ApplicationName: widestring);
324 destructor Destroy; override;
325 procedure GetKeyboardKeyLabels(const List: TStrings);
326 procedure GetKeyboardKeyNames(const List: TStrings);
327 procedure GetKeyboardKeyInfos(const List: TStrings);
328 procedure PollRotation;
329 procedure PollTranslation;
330 procedure PollKeys;
331 procedure Poll;
332 { Get current rotation, with angle in radians. }
333 procedure GetSensorRotation(var X, Y, Z, Angle: Double);
334 procedure GetSensorTranslation(var X, Y, Z, Length: Double);
335 procedure SetExampleCustomEmulations;
336 procedure SetCustomEmulationAxis(const AIndex: TEmulationAxis; const ADetail: TEmulationDetail; const AKeyLeft, AKeyRight: byte; const AThreshold, AMultiplier: integer);
337 procedure CopyToSettings(var Settings: TEmulationSettings);
338 procedure SetFromSettings(const Settings: TEmulationSettings);
339 property CustomEmulations[AIndex: TEmulationAxis]: TEmulationAxisSetting read GetCustomEmulations write SetCustomEmulations;
340 //published
341 property EmulationType: TEmulationType read FEmulationType write FEmulationType;
342 property LastResult: HResult read FLastResult;
343 property Loaded: boolean read FLoaded;
344 property RevisionNumber: widestring read FRevisionNumber;
345 property Sensor: ISensor read FSensor;
346 property SensorPeriod: Double read GetSensorPeriod;
347 property Keyboard: IKeyboard read FKeyboard;
348 property KeyboardEmulationThreshold: integer read FKeyboardEmulationThreshold write FKeyboardEmulationThreshold;
349 property OnKeyChange: TOnKeyChange read FOnKeyChange write FOnKeyChange;
350 property OnKeyUp: TOnKeySomething read FOnKeyUp write FOnKeyUp;
351 property OnKeyDown: TOnKeySomething read FOnKeyDown write FOnKeyDown;
352 property OnRotation: TOnRotation read FOnRotation write FOnRotation;
353 property OnTranslation: TOnTranslation read FOnTranslation write FOnTranslation;
354 end;
355
356 procedure LoadSchemeFromFile(var Scheme: TEmulationSettings; const Filename: string);
357 procedure SaveSchemeToFile(const Scheme: TEmulationSettings; const Filename: string);
358
359 implementation
360
361 uses
362 IniFiles, Math;
363
364 const
365 {$EXTERNALSYM MOUSEEVENTF_HWHEEL}
366 MOUSEEVENTF_HWHEEL = $1000; { wheel button rolled }
367 MOUSEEVENTF_WHEEL = 128; // JA
368
369
370 { T3DConnexionDevice }
371
372 {*------------------------------------------------------------------------------
373 Constructor; connectes to COM object, loads preferences. Sets Loaded boolean.
374
375 @param ApplicationName Name of application; used for preferences.
376 ------------------------------------------------------------------------------*}
377 procedure T3DConnexionDevice.CopyToSettings(var Settings: TEmulationSettings);
378 var axis: TEmulationAxis;
379 begin
380 for axis := Low(TEmulationAxis) to High(TEmulationAxis)
381 do Settings[axis] := CustomEmulations[axis];
382 end;
383
384 constructor T3DConnexionDevice.Create(const ApplicationName: widestring);
385 var iHelper: IUnknown;
386 info: ITDxInfo;
387 iKey: integer;
388 axis: TEmulationAxis;
389 begin
390 inherited Create;
391 FLoaded := false;
392 CoInitialize(nil);
393 FLastResult := CoCreateInstance(CLASS_TDxInfo, nil, CLSCTX_INPROC_SERVER, ITDxInfo, iHelper);
394 if (FLastResult <> S_OK)
395 then Exit;
396 FLastResult := iHelper.QueryInterface(ITDxInfo, info);
397 if FLastResult = S_OK
398 then FRevisionNumber := info.RevisionNumber;
399 FLastResult := CoCreateInstance(CLASS_Device, nil, CLSCTX_INPROC_SERVER, ISimpleDevice, iHelper);
400 if (FLastResult <> S_OK)
401 then Exit;
402 FLastResult := iHelper.QueryInterface(ISimpleDevice, FDevice);
403 if (FLastResult <> S_OK)
404 then Exit;
405 FDevice.Connect;
406 FDevice.LoadPreferences(ApplicationName);
407 FSensor := FDevice.Sensor;
408 FKeyboard := FDevice.Keyboard;
409 SetLength(FKeyStates, FKeyboard.Keys);
410 for iKey := Low(FKeyStates) to High(FKeyStates)
411 do FKeyStates[iKey] := false;
412 for axis := Low(FEmulatedKeyPressedStates) to High(FEmulatedKeyPressedStates) do begin
413 FEmulatedKeyPressedStates[axis, true] := false;
414 FEmulatedKeyPressedStates[axis, false] := false;
415 end;
416 FLoaded := true;
417 FEmulationType := etNone;
418 FKeyboardEmulationThreshold := 60;
419 SetExampleCustomEmulations;
420 end;
421
422 {*------------------------------------------------------------------------------
423 Destructor
424 ------------------------------------------------------------------------------*}
425 destructor T3DConnexionDevice.Destroy;
426 begin
427 CoUninitialize;
428 inherited;
429 end;
430
431 {*------------------------------------------------------------------------------
432 Fires an event about a changed key status.
433
434 @param KeyIndex Index of key that was pressed, 0..n-1
435 @param Pressed Whether key is currently down
436 ------------------------------------------------------------------------------*}
437 procedure T3DConnexionDevice.FireKeyChange(const KeyIndex: integer;
438 const Pressed: boolean);
439 var keyFlag: cardinal;
440 begin
441 if Assigned(FOnKeyChange) then
442 FOnKeyChange(KeyIndex, Pressed);
443 if Pressed then
444 begin
445 if Assigned(FOnKeyDown) then FOnKeyDown(KeyIndex);
446 end else
447 begin
448 if Assigned(FOnKeyUp) then FOnKeyUp(KeyIndex);
449 end;
450 if FEmulationType in [etMouse, etMouseKeyboard] then
451 begin
452 if KeyIndex = 1 then
453 begin
454 if Pressed then
455 KeyFlag := MOUSEEVENTF_RIGHTDOWN
456 else
457 KeyFlag := MOUSEEVENTF_RIGHTUP;
458 end else
459 begin
460 if Pressed then
461 KeyFlag := MOUSEEVENTF_LEFTDOWN
462 else
463 KeyFlag := MOUSEEVENTF_LEFTUP;
464 end;
465 Mouse_Event(KeyFlag, 0, 0, 0, 0);
466 end;
467 end;
468
469 {*------------------------------------------------------------------------------
470 Fires an event that notifies about rotations.
471
472 @param X X axis (tilt forward/backwards)
473 @param Y Y axis (rotate)
474 @param Z Z axis (tilt sidewards)
475 @param Angle Angle of rotation.
476 ------------------------------------------------------------------------------*}
477 procedure T3DConnexionDevice.FireRotation(const X, Y, Z, Angle: Double);
478 var cX, cZ: cardinal;
479 begin
480 if Assigned(FOnRotation)
481 then FOnRotation(X, Y, Z, Angle);
482 case FEmulationType of
483 etNone: ;
484 etMouse, etMouseKeyboard: begin
485 cX := cardinal(Round(-X*16));
486 cZ := cardinal(Round(-Z*16));
487 if cX<>0
488 then Mouse_Event(MOUSEEVENTF_WHEEL, 0, 0, cX, 0);
489 if cZ<>0
490 then Mouse_Event(MOUSEEVENTF_HWHEEL, 0, 0, cZ, 0);
491 end;
492 etKeyboard: ;
493 etCustom: begin
494 HandleCustomDetail(eaRotateX, X);
495 HandleCustomDetail(eaRotateY, Y);
496 HandleCustomDetail(eaRotateZ, Z);
497 HandleCustomDetail(eaRotateAngle, Angle);
498 end;
499 end;
500 end;
501
502 {*------------------------------------------------------------------------------
503 Fires an event that notifies about rotations.
504
505 @param Rotation Rotation object.
506 ------------------------------------------------------------------------------*}
507 procedure T3DConnexionDevice.FireRotation(const Rotation: IAngleAxis);
508 begin
509 FireRotation(Rotation.X, Rotation.Y, Rotation.Z, Rotation.Angle);
510 end;
511
512 {*------------------------------------------------------------------------------
513 Fires an event that notifies about translations.
514
515 @param X X axis (move left/right)
516 @param Y Y axis (move up/down)
517 @param Z Z axis (move forward/backwards)
518 @param Length Length of the vector consisting of the above.
519 ------------------------------------------------------------------------------*}
520 procedure T3DConnexionDevice.FireTranslation(const X, Y, Z, Length: Double);
521 var cX, cY, cZ: integer;
522 begin
523 if Assigned(FOnTranslation)
524 then FOnTranslation(X, Y, Z, Length);
525 case FEmulationType of
526 etNone: ;
527 etMouse: Mouse_Event(MOUSEEVENTF_MOVE, Round(X), Round(Z), 0, 0);
528 etKeyboard, etMouseKeyboard: begin
529 { DONE -oPatrick : Implement keyboard rotation emulation }
530 cX := Round(X); // arrow left/right
531 if cX>FKeyboardEmulationThreshold
532 then Keybd_Event(VK_RIGHT, 0, 0, 0)
533 else if cX<-FKeyboardEmulationThreshold
534 then Keybd_Event(VK_LEFT, 0, 0, 0)
535 else begin
536 Keybd_Event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
537 Keybd_Event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
538 end;
539 cZ := Round(Z); // arrow up/down
540 if cZ>FKeyboardEmulationThreshold
541 then Keybd_Event(VK_DOWN, 0, 0, 0)
542 else if cZ<-FKeyboardEmulationThreshold
543 then Keybd_Event(VK_UP, 0, 0, 0)
544 else begin
545 Keybd_Event(VK_UP, 0, KEYEVENTF_KEYUP, 0);
546 Keybd_Event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
547 end;
548 cY := Round(Y); // page up/down
549 if cY>(3*FKeyboardEmulationThreshold)
550 then Keybd_Event(VK_PRIOR, 0, 0, 0)
551 else if cY<-(3*FKeyboardEmulationThreshold)
552 then Keybd_Event(VK_NEXT, 0, 0, 0)
553 else begin
554 Keybd_Event(VK_PRIOR, 0, KEYEVENTF_KEYUP, 0);
555 Keybd_Event(VK_NEXT, 0, KEYEVENTF_KEYUP, 0);
556 end;
557 end;
558 etCustom: begin
559 HandleCustomDetail(eaTranslateX, X);
560 HandleCustomDetail(eaTranslateY, Y);
561 HandleCustomDetail(eaTranslateZ, Z);
562 HandleCustomDetail(eaTranslateLength, Length);
563 end;
564 end;
565 end;
566
567 {*------------------------------------------------------------------------------
568 Fires an event that notifies about translations.
569
570 @param Translation Translation object.
571 ------------------------------------------------------------------------------*}
572 procedure T3DConnexionDevice.FireTranslation(const Translation: IVector3D);
573 begin
574 FireTranslation(Translation.X, Translation.Y, Translation.Z, Translation.Length);
575 end;
576
577 function T3DConnexionDevice.GetCustomEmulations(AIndex: TEmulationAxis): TEmulationAxisSetting;
578 begin
579 Result := FCustomEmulations[AIndex];
580 end;
581
582 {*------------------------------------------------------------------------------
583 Returns labels and names of device keys/buttons.
584
585 @param List List that will receive information.
586 ------------------------------------------------------------------------------*}
587 procedure T3DConnexionDevice.GetKeyboardKeyInfos(const List: TStrings);
588 var iKey: integer;
589 s: widestring;
590 begin
591 if not Assigned(FDevice)
592 then Exit;
593 if not Assigned(FKeyboard)
594 then Exit;
595 for iKey := 1 to FKeyboard.Keys do begin
596 try
597 s := FKeyboard.GetKeyLabel(iKey) + '=' + FKeyboard.GetKeyName(iKey);
598 except
599 on E: Exception do s := UTF8Decode(E.Message);
600 end;
601 List.Add(UTF8Encode(s));
602 end;
603 end;
604
605 {*------------------------------------------------------------------------------
606 Returns labels of device keys/buttons.
607
608 @param List List that will receive information.
609 ------------------------------------------------------------------------------*}
610 procedure T3DConnexionDevice.GetKeyboardKeyLabels(const List: TStrings);
611 var iKey: integer;
612 s: widestring;
613 begin
614 if not Assigned(FDevice)
615 then Exit;
616 if not Assigned(FKeyboard)
617 then Exit;
618 for iKey := 1 to FKeyboard.Keys do begin
619 try
620 s := FKeyboard.GetKeyLabel(iKey);
621 except
622 on E: Exception do s := UTF8Decode('FKeyboard.GetKeyLabel: ' + E.Message);
623 end;
624 List.Add(UTF8Encode(s));
625 end;
626 end;
627
628 {*------------------------------------------------------------------------------
629 Returns names of device keys/buttons.
630
631 @param List List that will receive information.
632 ------------------------------------------------------------------------------*}
633 procedure T3DConnexionDevice.GetKeyboardKeyNames(const List: TStrings);
634 var iKey: integer;
635 s: widestring;
636 begin
637 if not Assigned(FDevice)
638 then Exit;
639 if not Assigned(FKeyboard)
640 then Exit;
641 for iKey := 1 to FKeyboard.Keys do begin
642 try
643 s := FKeyboard.GetKeyName(iKey);
644 except
645 on E: Exception do s := UTF8Decode('FKeyboard.GetKeyLabel: ' + E.Message);
646 end;
647 List.Add(UTF8Encode(s));
648 end;
649 end;
650
651 {*------------------------------------------------------------------------------
652 Returns the sensor period.
653 This may be the time difference between two polls?
654
655 @return Period.
656 ------------------------------------------------------------------------------*}
657 function T3DConnexionDevice.GetSensorPeriod: Double;
658 begin
659 if Assigned(FSensor)
660 then Result := FSensor.Period
661 else Result := 0;
662 end;
663
664 procedure T3DConnexionDevice.HandleCustomDetail(const Axis: TEmulationAxis;
665 const Value: Double);
666 var AxisSettings: TEmulationAxisSetting;
667 procedure HandleKeyboardEvent(KeyLeft, KeyRight: byte);
668 var cValue: integer;
669 bPressed: boolean;
670 begin
671 cValue := Round(Value * AxisSettings.Multiplier);
672 bPressed := Abs(cValue) > AxisSettings.Threshold;
673 // Get rid of old states first
674 if (FEmulatedKeyPressedStates[Axis, true] and ((cValue<=0) or (not bPressed))) then begin
675 // was: positive amplitude, now too small, zero or negative.
676 FEmulatedKeyPressedStates[Axis, true] := false;
677 Keybd_Event(KeyLeft, 0, KEYEVENTF_KEYUP, 0);
678 end else if (FEmulatedKeyPressedStates[Axis, false] and ((cValue>=0) or (not bPressed))) then begin
679 // was: negative amplitude, now too small, zero or positive.
680 FEmulatedKeyPressedStates[Axis, false] := false;
681 Keybd_Event(KeyRight, 0, KEYEVENTF_KEYUP, 0);
682 end;
683 if not bPressed
684 then Exit;
685 // Now enter new states:
686 if cValue>0 then begin
687 FEmulatedKeyPressedStates[Axis, true] := true;
688 Keybd_Event(KeyLeft, 0, 0, 0);
689 end else begin
690 FEmulatedKeyPressedStates[Axis, false] := true;
691 Keybd_Event(KeyRight, 0, 0, 0);
692 end;
693 end;
694 begin
695 AxisSettings := CustomEmulations[Axis];
696 case AxisSettings.Detail of
697 edNone: ;
698 edMouseX: Mouse_Event(MOUSEEVENTF_MOVE, cardinal(Round(Value*AxisSettings.Multiplier)), 0, 0, 0);
699 edMouseY: Mouse_Event(MOUSEEVENTF_MOVE, 0, cardinal(Round(Value*AxisSettings.Multiplier)), 0, 0);
700 edMouseWheelV: Mouse_Event(MOUSEEVENTF_WHEEL, 0, 0, cardinal(Round(-Value*AxisSettings.Multiplier)), 0);
701 edMouseWheelH: Mouse_Event(MOUSEEVENTF_HWHEEL, 0, 0, cardinal(Round(-Value*AxisSettings.Multiplier)), 0);
702 edKeyboardArrowsLR: HandleKeyboardEvent(VK_RIGHT, VK_LEFT);
703 edKeyboardArrowsUD: HandleKeyboardEvent(VK_DOWN, VK_UP);
704 edKeyboardPageUD: HandleKeyboardEvent(VK_PRIOR, VK_NEXT);
705 edKeyboardCustom: HandleKeyboardEvent(AxisSettings.KeyLeft, AxisSettings.KeyRight);
706 end;
707 end;
708
709 {*------------------------------------------------------------------------------
710 Polls information on rotation, translation and key stati.
711 Information will be returned in corresponding events.
712 ------------------------------------------------------------------------------*}
713 procedure T3DConnexionDevice.Poll;
714 begin
715 PollRotation;
716 PollTranslation;
717 PollKeys;
718 end;
719
720 {*------------------------------------------------------------------------------
721 Polls information on key stati.
722 Information will be returned in corresponding event.
723 ------------------------------------------------------------------------------*}
724 procedure T3DConnexionDevice.PollKeys;
725 var iKey: integer;
726 begin
727 for iKey := Low(FKeyStates) to High(FKeyStates) do try
728 case FKeyStates[iKey] of
729 true: if FKeyboard.IsKeyUp(Succ(iKey)) then begin
730 FKeyStates[iKey] := false;
731 FireKeyChange(iKey, false);
732 end;
733 false: if FKeyboard.IsKeyDown(Succ(iKey)) then begin
734 FKeyStates[iKey] := true;
735 FireKeyChange(iKey, true);
736 end;
737 end;
738 except
739 end;
740 end;
741
742 {*------------------------------------------------------------------------------
743 Polls information on rotation.
744 Information will be returned in corresponding event.
745 ------------------------------------------------------------------------------*}
746 procedure T3DConnexionDevice.PollRotation;
747 var rotation: IAngleAxis;
748 begin
749 rotation := FSensor.Rotation;
750 FireRotation(rotation);
751 end;
752
753 {*------------------------------------------------------------------------------
754 Polls information on translation.
755 Information will be returned in corresponding event.
756 ------------------------------------------------------------------------------*}
757 procedure T3DConnexionDevice.PollTranslation;
758 var translation: IVector3D;
759 begin
760 translation := FSensor.Translation;
761 FireTranslation(translation);
762 end;
763
764 procedure T3DConnexionDevice.GetSensorRotation(var X, Y, Z, Angle: Double);
765 var rotation: IAngleAxis;
766 begin
767 rotation := FSensor.Rotation;
768 X := rotation.X;
769 Y := rotation.Y;
770 Z := rotation.Z;
771 Angle := DegToRad(rotation.Angle);
772 end;
773
774 procedure T3DConnexionDevice.GetSensorTranslation(var X, Y, Z, Length: Double);
775 var translation: IVector3D;
776 begin
777 translation := FSensor.Translation;
778 X := translation.X;
779 Y := translation.Y;
780 Z := translation.Z;
781 Length := translation.Length;
782 end;
783
784 procedure T3DConnexionDevice.SetCustomEmulationAxis(const AIndex: TEmulationAxis;
785 const ADetail: TEmulationDetail; const AKeyLeft, AKeyRight: byte;
786 const AThreshold, AMultiplier: integer);
787 begin
788 with FCustomEmulations[AIndex] do begin
789 Detail := ADetail;
790 KeyLeft := AKeyLeft;
791 KeyRight := AKeyRight;
792 Threshold := AThreshold;
793 Multiplier := AMultiplier;
794 end;
795 end;
796
797 procedure T3DConnexionDevice.SetCustomEmulations(AIndex: TEmulationAxis;
798 const Value: TEmulationAxisSetting);
799 begin
800 FCustomEmulations[AIndex] := Value;
801 end;
802
803 procedure T3DConnexionDevice.SetExampleCustomEmulations;
804 begin
805 SetCustomEmulationAxis(eaRotateX, edMouseWheelV, 0, 0, 0, 16);
806 SetCustomEmulationAxis(eaRotateY, edNone, 0, 0, 0, 1);
807 SetCustomEmulationAxis(eaRotateZ, edMouseWheelH, 0, 0, 0, 16);
808 SetCustomEmulationAxis(eaRotateAngle, edNone, 0, 0, 0, 1);
809 SetCustomEmulationAxis(eaTranslateX, edKeyboardArrowsLR, 0, 0, 60, 1);
810 SetCustomEmulationAxis(eaTranslateY, edKeyboardPageUD, 0, 0, 60, 1);
811 SetCustomEmulationAxis(eaTranslateZ, edKeyboardArrowsUD, 0, 0, 60, 1);
812 SetCustomEmulationAxis(eaTranslateLength, edNone, 0, 0, 0, 1);
813 end;
814
815 procedure T3DConnexionDevice.SetFromSettings(const Settings: TEmulationSettings);
816 var axis: TEmulationAxis;
817 begin
818 for axis := Low(TEmulationAxis) to High(TEmulationAxis)
819 do CustomEmulations[axis] := Settings[axis];
820 end;
821
822 procedure LoadSchemeFromFile(var Scheme: TEmulationSettings; const Filename: string);
823 var mif: TMemIniFile;
824 axis: TEmulationAxis;
825 sSection: string;
826 begin
827 mif := TMemIniFile.Create(Filename);
828 for axis := Low(TEmulationAxis) to High(TEmulationAxis) do begin
829 sSection := SEmulationAxisIDs[axis];
830 try
831 Scheme[axis].Detail := TEmulationDetail(mif.ReadInteger(sSection, 'Detail', integer(Scheme[axis].Detail)));
832 except
833 Scheme[axis].Detail := edNone;
834 end;
835 try
836 Scheme[axis].KeyLeft := mif.ReadInteger(sSection, 'KeyLeft', Scheme[axis].KeyLeft);
837 except
838 Scheme[axis].KeyLeft := 0;
839 end;
840 try
841 Scheme[axis].KeyRight := mif.ReadInteger(sSection, 'KeyRight', Scheme[axis].KeyRight);
842 except
843 Scheme[axis].KeyRight := 0;
844 end;
845 try
846 Scheme[axis].Threshold := mif.ReadInteger(sSection, 'Threshold', Scheme[axis].Threshold);
847 except
848 Scheme[axis].Threshold := 60;
849 end;
850 try
851 Scheme[axis].Multiplier := mif.ReadInteger(sSection, 'Multiplier', Scheme[axis].Multiplier);
852 except
853 Scheme[axis].Multiplier := 1;
854 end;
855 end;
856 mif.Free;
857 end;
858
859 procedure SaveSchemeToFile(const Scheme: TEmulationSettings; const Filename: string);
860 var mif: TMemIniFile;
861 axis: TEmulationAxis;
862 sSection: string;
863 begin
864 mif := TMemIniFile.Create(Filename);
865 for axis := Low(TEmulationAxis) to High(TEmulationAxis) do begin
866 sSection := SEmulationAxisIDs[axis];
867 mif.WriteInteger(sSection, 'Detail', integer(Scheme[axis].Detail));
868 mif.WriteInteger(sSection, 'KeyLeft', Scheme[axis].KeyLeft);
869 mif.WriteInteger(sSection, 'KeyRight', Scheme[axis].KeyRight);
870 mif.WriteInteger(sSection, 'Threshold', Scheme[axis].Threshold);
871 mif.WriteInteger(sSection, 'Multiplier', Scheme[axis].Multiplier);
872 end;
873 mif.UpdateFile;
874 mif.Free;
875 end;
876
877 {$else} // ifndef REAL_3D_CONNEXION_IMPLEMENTATION - dummy implementation
878
879 type
880 T3DConnexionDevice = class(TObject)
881 private
882 FLoaded: boolean; /// Whether connection to COM object was successful
883 public
884 constructor Create(const ApplicationName: widestring);
885 destructor Destroy; override;
886 procedure GetSensorRotation(var X, Y, Z, Angle: Double);
887 procedure GetSensorTranslation(var X, Y, Z, Length: Double);
888 //published
889 property Loaded: boolean read FLoaded;
890 end;
891
892 implementation
893
894 constructor T3DConnexionDevice.Create(const ApplicationName: widestring);
895 begin
896 inherited Create;
897 FLoaded := false;
898 end;
899
900 destructor T3DConnexionDevice.Destroy;
901 begin
902 inherited;
903 end;
904
905 procedure T3DConnexionDevice.GetSensorRotation(var X, Y, Z, Angle: Double);
906 begin
907 X := 0; Y := 0; Z := 0; Angle := 0;
908 end;
909
910 procedure T3DConnexionDevice.GetSensorTranslation(var X, Y, Z, Length: Double);
911 begin
912 X := 0; Y := 0; Z := 0; Length := 0;
913 end;
914
915 {$endif} // ifndef REAL_3D_CONNEXION_IMPLEMENTATION
916
917 end.