/usr/src/castle-game-engine-4.1.1/x3d/x3d_keydevicesensor.inc is in castle-game-engine-src 4.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {
Copyright 2008-2013 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" 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.
----------------------------------------------------------------------------
}
{$ifdef read_interface}
{ }
TAbstractKeyDeviceSensorNode = class(TAbstractSensorNode)
public
procedure CreateNode; override;
{ React to key down/up events.
This is used by TCastleSceneCore to communicate with this key sensor.
Semantics follow TInputPressRelease behavior for EventType = itKey.
This means that Key may be K_None (if not
representable as TKey) and C may be #0 (if not representable as char),
but not both --- at least one of Key or C must contains something useful.
Also, key presses are affected
by the "key repeat" feature of the OS (windowing system actually), so when
user holds down a key --- we get many key down messages.
@groupBegin }
procedure KeyDown(Key: TKey; C: char; const Time: TX3DTime); virtual;
procedure KeyUp(Key: TKey; C: char; const Time: TX3DTime); virtual;
{ @groupEnd }
end;
TKeySensorNode = class(TAbstractKeyDeviceSensorNode)
private
AltKey, ControlKey, ShiftKey: boolean;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFInt32, out } { }
private FEventActionKeyPress: TX3DEvent;
public property EventActionKeyPress: TX3DEvent read FEventActionKeyPress;
{ Event: SFInt32, out } { }
private FEventActionKeyRelease: TX3DEvent;
public property EventActionKeyRelease: TX3DEvent read FEventActionKeyRelease;
{ Event: SFBool, out } { }
private FEventAltKey: TX3DEvent;
public property EventAltKey: TX3DEvent read FEventAltKey;
{ Event: SFBool, out } { }
private FEventControlKey: TX3DEvent;
public property EventControlKey: TX3DEvent read FEventControlKey;
{ Event: SFString, out } { }
private FEventKeyPress: TX3DEvent;
public property EventKeyPress: TX3DEvent read FEventKeyPress;
{ Event: SFString, out } { }
private FEventKeyRelease: TX3DEvent;
public property EventKeyRelease: TX3DEvent read FEventKeyRelease;
{ Event: SFBool, out } { }
private FEventShiftKey: TX3DEvent;
public property EventShiftKey: TX3DEvent read FEventShiftKey;
procedure KeyDown(Key: TKey; C: char; const Time: TX3DTime); override;
procedure KeyUp(Key: TKey; C: char; const Time: TX3DTime); override;
end;
TStringSensorNode = class(TAbstractKeyDeviceSensorNode)
private
EnteredText: string;
Active: boolean;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdDeletionAllowed: TSFBool;
public property FdDeletionAllowed: TSFBool read FFdDeletionAllowed;
{ Event: SFString, out } { }
private FEventEnteredText: TX3DEvent;
public property EventEnteredText: TX3DEvent read FEventEnteredText;
{ Event: SFString, out } { }
private FEventFinalText: TX3DEvent;
public property EventFinalText: TX3DEvent read FEventFinalText;
procedure KeyDown(Key: TKey; C: char; const Time: TX3DTime); override;
end;
{$endif read_interface}
{$ifdef read_implementation}
procedure TAbstractKeyDeviceSensorNode.CreateNode;
begin
inherited;
DefaultContainerField := 'children';
end;
procedure TAbstractKeyDeviceSensorNode.KeyDown(Key: TKey; C: char; const Time: TX3DTime);
begin
{ Nothing to do in this class. }
end;
procedure TAbstractKeyDeviceSensorNode.KeyUp(Key: TKey; C: char; const Time: TX3DTime);
begin
{ Nothing to do in this class. }
end;
{ Convert TKey to VRML "action" key code.
As defined by X3D KeySensor node specification. }
function KeyToActionKey(Key: TKey; out ActionKey: Integer): boolean;
begin
Result := true;
case Key of
K_F1 .. K_F12 : ActionKey := Ord(Key) - Ord(K_F1) + 1;
K_Home : ActionKey := 13;
K_End : ActionKey := 14;
K_PageUp : ActionKey := 15;
K_PageDown : ActionKey := 16;
K_Up : ActionKey := 17;
K_Down : ActionKey := 18;
K_Left : ActionKey := 19;
K_Right : ActionKey := 20;
else Result := false;
end;
end;
procedure TKeySensorNode.CreateNode;
begin
inherited;
FEventActionKeyPress := TX3DEvent.Create(Self, 'actionKeyPress', TSFInt32, false);
Events.Add(FEventActionKeyPress);
FEventActionKeyRelease := TX3DEvent.Create(Self, 'actionKeyRelease', TSFInt32, false);
Events.Add(FEventActionKeyRelease);
FEventAltKey := TX3DEvent.Create(Self, 'altKey', TSFBool, false);
Events.Add(FEventAltKey);
FEventControlKey := TX3DEvent.Create(Self, 'controlKey', TSFBool, false);
Events.Add(FEventControlKey);
FEventKeyPress := TX3DEvent.Create(Self, 'keyPress', TSFString, false);
Events.Add(FEventKeyPress);
FEventKeyRelease := TX3DEvent.Create(Self, 'keyRelease', TSFString, false);
Events.Add(FEventKeyRelease);
FEventShiftKey := TX3DEvent.Create(Self, 'shiftKey', TSFBool, false);
Events.Add(FEventShiftKey);
DefaultContainerField := 'children';
end;
class function TKeySensorNode.ClassNodeTypeName: string;
begin
Result := 'KeySensor';
end;
class function TKeySensorNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TKeySensorNode.KeyDown(Key: TKey; C: char; const Time: TX3DTime);
procedure HandleSpecial(var SpecialPressed: boolean; SpecialEvent: TX3DEvent);
begin
if not SpecialPressed then
begin
SpecialEvent.Send(true, Time);
EventIsActive.Send(true, Time);
SpecialPressed := true;
end;
end;
var
ActionKey: Integer;
begin
inherited;
if FdEnabled.Value then
begin
{ For now, actionKeyPress and keyPress are simply always accompanied by isActive := true.
We ignore the trouble with key repeat, as spec doesn't say what to do
(suggesting that we should make event only on the actual press, but this
would be poor --- key repeat is useful for users).
We track ctrl / alt / shift state and avoid key repeat for them.
Reason: 1. for them, key repeat seems really not useful.
2. on Unix, windowing system (or at least GTK) doesn't even do key repeat
for them. So this makes Windows and Unix behave more commonly. }
if KeyToActionKey(Key, ActionKey) then
begin
EventActionKeyPress.Send(ActionKey, Time);
EventIsActive.Send(true, Time);
end;
if C <> #0 then
begin
EventKeyPress.Send(C, Time);
EventIsActive.Send(true, Time);
end;
case Key of
K_Alt : HandleSpecial(AltKey, EventAltKey);
K_Ctrl : HandleSpecial(ControlKey, EventControlKey);
K_Shift: HandleSpecial(ShiftKey, EventShiftKey);
end;
end;
end;
procedure TKeySensorNode.KeyUp(Key: TKey; C: char; const Time: TX3DTime);
procedure HandleSpecial(var SpecialPressed: boolean; SpecialEvent: TX3DEvent);
begin
if SpecialPressed then
begin
SpecialEvent.Send(false, Time);
EventIsActive.Send(false, Time);
SpecialPressed := false;
end;
end;
var
ActionKey: Integer;
begin
if FdEnabled.Value then
begin
if KeyToActionKey(Key, ActionKey) then
begin
EventActionKeyRelease.Send(ActionKey, Time);
EventIsActive.Send(false, Time);
end;
if C <> #0 then
begin
EventKeyRelease.Send(C, Time);
EventIsActive.Send(false, Time);
end;
case Key of
K_Alt : HandleSpecial(AltKey, EventAltKey);
K_Ctrl : HandleSpecial(ControlKey, EventControlKey);
K_Shift: HandleSpecial(ShiftKey, EventShiftKey);
end;
end;
inherited;
end;
procedure TStringSensorNode.CreateNode;
begin
inherited;
FFdDeletionAllowed := TSFBool.Create(Self, 'deletionAllowed', true);
Fields.Add(FFdDeletionAllowed);
FEventEnteredText := TX3DEvent.Create(Self, 'enteredText', TSFString, false);
Events.Add(FEventEnteredText);
FEventFinalText := TX3DEvent.Create(Self, 'finalText', TSFString, false);
Events.Add(FEventFinalText);
DefaultContainerField := 'children';
end;
class function TStringSensorNode.ClassNodeTypeName: string;
begin
Result := 'StringSensor';
end;
class function TStringSensorNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TStringSensorNode.KeyDown(Key: TKey; C: char; const Time: TX3DTime);
begin
inherited;
if FdEnabled.Value and ( (C <> #0) or (Key in [K_BackSpace, K_Enter]) ) then
begin
if not Active then
begin
Active := true;
EventIsActive.Send(Active, Time);
end;
{ Catch both CharBackspace and K_BackSpace.
Catching CharBackspace is not enough, because it's also equal to Ctrl+H.
Same for CharEnter: it's also equal to Ctrl+M.
And Ctrl+... may be often used by some menu items, see e.g. view3dscene. }
if (C = CharBackspace) or (Key = K_BackSpace) then
begin
if FdDeletionAllowed.Value and (EnteredText <> '') then
begin
SetLength(EnteredText, Length(EnteredText) - 1);
EventEnteredText.Send(EnteredText, Time);
end;
end else
if (C = CharEnter) or (Key = K_Enter) then
begin
EventFinalText.Send(EnteredText, Time);
{ X3D spec says explicitly that in this case, enteredText event
is not generated although internal value is empty. }
EnteredText := '';
Active := false;
EventIsActive.Send(Active, Time);
end else
begin
EnteredText := EnteredText + C;
EventEnteredText.Send(EnteredText, Time);
end;
end;
end;
procedure RegisterKeyDeviceSensorNodes;
begin
NodesManager.RegisterNodeClasses([
TKeySensorNode,
TStringSensorNode
]);
end;
{$endif read_implementation}
|