| 1 |
{ |
| 2 |
Copyright 2001-2024 Michalis Kamburelis, Tomasz Wojtyś. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Types and constants to handle keys and mouse. |
| 17 |
They are used throughout our engine, both by CastleControl (Lazarus component) |
| 18 |
and by non-Lazarus CastleWindow. } |
| 19 |
unit CastleKeysMouse; |
| 20 |
|
| 21 |
{$I castleconf.inc} |
| 22 |
{$ifdef FPC} {$modeswitch advancedrecords} {$endif} |
| 23 |
|
| 24 |
interface |
| 25 |
|
| 26 |
uses Classes, Generics.Collections, |
| 27 |
CastleUtils, CastleStringUtils, CastleVectors, CastleXMLConfig; |
| 28 |
|
| 29 |
type |
| 30 |
{ Keys on the keyboard. |
| 31 |
|
| 32 |
Some properties of keyXxx constants that are guaranteed: |
| 33 |
|
| 34 |
@unorderedList( |
| 35 |
@item(keyNone means "no key". It's guaranteed that it's always equal to zero.) |
| 36 |
|
| 37 |
@item(Letters (constants keyA .. keyZ) are guaranteed to be always equal to |
| 38 |
TKey('A') .. TKey('Z') and digits (constants key0 .. key9) are |
| 39 |
guaranteed to be always equal to TKey('0') .. TKey('9'). |
| 40 |
That is, their ordinal values are equal to their ASCII codes, |
| 41 |
and they are always ordered. |
| 42 |
|
| 43 |
Also keyF1 .. keyF12 (function keys) are guaranteed to be always nicely ordered |
| 44 |
(i.e. keyF2 = keyF1 + 1, keyF3 = keyF2 + 1 and so on). |
| 45 |
|
| 46 |
Also keyEscape, keyBackSpace, keyTab, keyEnter are guaranteed to be always equal |
| 47 |
to CharEscape, CharBackSpace, CharTab, CharEnter (well, typecasted to |
| 48 |
TKey type).) |
| 49 |
) |
| 50 |
|
| 51 |
Do not ever use keyReserved_Xxx for any purpose, they may be used |
| 52 |
for something in next CGE versions. |
| 53 |
} |
| 54 |
TKey = ( |
| 55 |
keyNone, |
| 56 |
keyPrintScreen, |
| 57 |
keyCapsLock, |
| 58 |
keyScrollLock, |
| 59 |
keyNumLock, |
| 60 |
keyPause, |
| 61 |
keyApostrophe, |
| 62 |
keySemicolon, |
| 63 |
keyBackSpace, //< = Ord(CharBackSpace) = 8 |
| 64 |
keyTab, //< = Ord(CharTab) = 9 |
| 65 |
keySlash, |
| 66 |
keyBackQuote, |
| 67 |
keyMinus, |
| 68 |
keyEnter, //< = Ord(CharEnter) = 13 |
| 69 |
keyEqual, |
| 70 |
keyBackSlash, |
| 71 |
keyShift, |
| 72 |
keyCtrl, |
| 73 |
keyAlt, |
| 74 |
keyPlus, |
| 75 |
keyReserved_20, |
| 76 |
keyReserved_21, |
| 77 |
keyReserved_22, |
| 78 |
keyReserved_23, |
| 79 |
keyReserved_24, |
| 80 |
keyReserved_25, |
| 81 |
keyReserved_26, |
| 82 |
keyEscape, //< = Ord(CharEscape) = 27 |
| 83 |
keyReserved_28, |
| 84 |
keyReserved_29, |
| 85 |
keyReserved_30, |
| 86 |
keyReserved_31, |
| 87 |
keySpace, //< = Ord(' ') = 32 |
| 88 |
keyPageUp, |
| 89 |
keyPageDown, |
| 90 |
keyEnd, |
| 91 |
keyHome, |
| 92 |
keyArrowLeft, |
| 93 |
keyArrowUp, |
| 94 |
keyArrowRight, |
| 95 |
keyArrowDown, |
| 96 |
keyReserved_41, |
| 97 |
keyReserved_42, |
| 98 |
keyReserved_43, |
| 99 |
keyReserved_44, |
| 100 |
keyInsert, |
| 101 |
keyDelete, |
| 102 |
keyReserved_47, |
| 103 |
key0, //< = Ord('0') = 48 |
| 104 |
key1, //< = Ord('1') |
| 105 |
key2, //< = Ord('2') |
| 106 |
key3, //< = Ord('3') |
| 107 |
key4, //< = Ord('4') |
| 108 |
key5, //< = Ord('5') |
| 109 |
key6, //< = Ord('6') |
| 110 |
key7, //< = Ord('7') |
| 111 |
key8, //< = Ord('8') |
| 112 |
key9, //< = Ord('9') = 57 |
| 113 |
keyReserved_58, |
| 114 |
keyReserved_59, |
| 115 |
keyReserved_60, |
| 116 |
keyReserved_61, |
| 117 |
keyReserved_62, |
| 118 |
keyReserved_63, |
| 119 |
keyReserved_64, |
| 120 |
keyA, //< = Ord('A') = 65 |
| 121 |
keyB, //< = Ord('B') |
| 122 |
keyC, //< = Ord('C') |
| 123 |
keyD, //< = Ord('D') |
| 124 |
keyE, //< = Ord('E') |
| 125 |
keyF, //< = Ord('F') |
| 126 |
keyG, //< = Ord('G') |
| 127 |
keyH, //< = Ord('H') |
| 128 |
keyI, //< = Ord('I') |
| 129 |
keyJ, //< = Ord('J') |
| 130 |
keyK, //< = Ord('K') |
| 131 |
keyL, //< = Ord('L') |
| 132 |
keyM, //< = Ord('M') |
| 133 |
keyN, //< = Ord('N') |
| 134 |
keyO, //< = Ord('O') |
| 135 |
keyP, //< = Ord('P') |
| 136 |
keyQ, //< = Ord('Q') |
| 137 |
keyR, //< = Ord('R') |
| 138 |
keyS, //< = Ord('S') |
| 139 |
keyT, //< = Ord('T') |
| 140 |
keyU, //< = Ord('U') |
| 141 |
keyV, //< = Ord('V') |
| 142 |
keyW, //< = Ord('W') |
| 143 |
keyX, //< = Ord('X') |
| 144 |
keyY, //< = Ord('Y') |
| 145 |
keyZ, //< = Ord('Z') = 90 |
| 146 |
keyLeftBracket, |
| 147 |
keyReserved_92, |
| 148 |
keyRightBracket, |
| 149 |
keyReserved_94, |
| 150 |
keyReserved_95, |
| 151 |
keyReserved_96, |
| 152 |
keyReserved_97, |
| 153 |
keyReserved_98, |
| 154 |
keyReserved_99, |
| 155 |
keyReserved_100, |
| 156 |
keyReserved_101, |
| 157 |
keyReserved_102, |
| 158 |
keyReserved_103, |
| 159 |
keyReserved_104, |
| 160 |
keyReserved_105, |
| 161 |
keyReserved_106, |
| 162 |
keyNumpadPlus , |
| 163 |
keyReserved_108, |
| 164 |
keyNumpadMinus, |
| 165 |
keyReserved_110, |
| 166 |
keyReserved_111, |
| 167 |
keyF1, |
| 168 |
keyF2, |
| 169 |
keyF3, |
| 170 |
keyF4, |
| 171 |
keyF5, |
| 172 |
keyF6, |
| 173 |
keyF7, |
| 174 |
keyF8, |
| 175 |
keyF9, |
| 176 |
keyF10, |
| 177 |
keyF11, |
| 178 |
keyF12, |
| 179 |
keyReserved_124, |
| 180 |
keyReserved_125, |
| 181 |
keyReserved_126, |
| 182 |
keyReserved_127, |
| 183 |
keyReserved_128, |
| 184 |
keyReserved_129, |
| 185 |
keyReserved_130, |
| 186 |
keyReserved_131, |
| 187 |
keyReserved_132, |
| 188 |
keyReserved_133, |
| 189 |
keyReserved_134, |
| 190 |
keyReserved_135, |
| 191 |
keyReserved_136, |
| 192 |
keyReserved_137, |
| 193 |
keyReserved_138, |
| 194 |
keyReserved_139, |
| 195 |
keyNumpad0, |
| 196 |
keyNumpad1, |
| 197 |
keyNumpad2, |
| 198 |
keyNumpad3, |
| 199 |
keyNumpad4, |
| 200 |
keyNumpad5, |
| 201 |
keyNumpad6, |
| 202 |
keyNumpad7, |
| 203 |
keyNumpad8, |
| 204 |
keyNumpad9, |
| 205 |
keyNumpadEnd, |
| 206 |
keyNumpadDown, |
| 207 |
keyNumpadPageDown, |
| 208 |
keyNumpadLeft, |
| 209 |
keyNumpadBegin, |
| 210 |
keyNumpadRight, |
| 211 |
keyNumpadHome, |
| 212 |
keyNumpadUp, |
| 213 |
keyNumpadPageUp, |
| 214 |
keyNumpadInsert, |
| 215 |
keyNumpadDelete, |
| 216 |
keyNumpadEnter, |
| 217 |
keyNumpadMultiply, |
| 218 |
keyNumpadDivide, |
| 219 |
{ Keys on Nintendo Switch pad. } |
| 220 |
keyPadA, |
| 221 |
keyPadB, |
| 222 |
keyPadX, |
| 223 |
keyPadY, |
| 224 |
keyPadL, |
| 225 |
keyPadR, |
| 226 |
keyPadZL, |
| 227 |
keyPadZR, |
| 228 |
keyPadPlus, |
| 229 |
keyPadMinus, |
| 230 |
keyPadLeft, |
| 231 |
keyPadUp, |
| 232 |
keyPadRight, |
| 233 |
keyPadDown, |
| 234 |
keyReserved_178, |
| 235 |
keyReserved_179, |
| 236 |
keyReserved_180, |
| 237 |
keyReserved_181, |
| 238 |
keyReserved_182, |
| 239 |
keyReserved_183, |
| 240 |
keyReserved_184, |
| 241 |
keyReserved_185, |
| 242 |
keyReserved_186, |
| 243 |
keyReserved_187, |
| 244 |
keyComma, |
| 245 |
keyReserved_189, |
| 246 |
keyPeriod, |
| 247 |
keyReserved_191 |
| 248 |
); |
| 249 |
|
| 250 |
{ Old key names (K_Xxx instead of keyXxx). } |
| 251 |
|
| 252 |
const |
| 253 |
K_None = keyNone deprecated; |
| 254 |
K_PrintScreen = keyPrintScreen deprecated; |
| 255 |
K_CapsLock = keyCapsLock deprecated; |
| 256 |
K_ScrollLock = keyScrollLock deprecated; |
| 257 |
K_NumLock = keyNumLock deprecated; |
| 258 |
K_Pause = keyPause deprecated; |
| 259 |
K_Apostrophe = keyApostrophe deprecated; |
| 260 |
K_Semicolon = keySemicolon deprecated; |
| 261 |
K_BackSpace = keyBackSpace deprecated; |
| 262 |
K_Tab = keyTab deprecated; |
| 263 |
K_Slash = keySlash deprecated; |
| 264 |
K_BackQuote = keyBackQuote deprecated; |
| 265 |
K_Minus = keyMinus deprecated; |
| 266 |
K_Enter = keyEnter deprecated; |
| 267 |
K_Equal = keyEqual deprecated; |
| 268 |
K_BackSlash = keyBackSlash deprecated; |
| 269 |
K_Shift = keyShift deprecated; |
| 270 |
K_Ctrl = keyCtrl deprecated; |
| 271 |
K_Alt = keyAlt deprecated; |
| 272 |
K_Plus = keyPlus deprecated; |
| 273 |
K_Escape = keyEscape deprecated; |
| 274 |
K_Space = keySpace deprecated; |
| 275 |
K_PageUp = keyPageUp deprecated; |
| 276 |
K_PageDown = keyPageDown deprecated; |
| 277 |
K_End = keyEnd deprecated; |
| 278 |
K_Home = keyHome deprecated; |
| 279 |
K_Left = keyArrowLeft deprecated; |
| 280 |
K_Up = keyArrowUp deprecated; |
| 281 |
K_Right = keyArrowRight deprecated; |
| 282 |
K_Down = keyArrowDown deprecated; |
| 283 |
K_Insert = keyInsert deprecated; |
| 284 |
K_Delete = keyDelete deprecated; |
| 285 |
K_0 = key0 deprecated; |
| 286 |
K_1 = key1 deprecated; |
| 287 |
K_2 = key2 deprecated; |
| 288 |
K_3 = key3 deprecated; |
| 289 |
K_4 = key4 deprecated; |
| 290 |
K_5 = key5 deprecated; |
| 291 |
K_6 = key6 deprecated; |
| 292 |
K_7 = key7 deprecated; |
| 293 |
K_8 = key8 deprecated; |
| 294 |
K_9 = key9 deprecated; |
| 295 |
K_A = keyA deprecated; |
| 296 |
K_B = keyB deprecated; |
| 297 |
K_C = keyC deprecated; |
| 298 |
K_D = keyD deprecated; |
| 299 |
K_E = keyE deprecated; |
| 300 |
K_F = keyF deprecated; |
| 301 |
K_G = keyG deprecated; |
| 302 |
K_H = keyH deprecated; |
| 303 |
K_I = keyI deprecated; |
| 304 |
K_J = keyJ deprecated; |
| 305 |
K_K = keyK deprecated; |
| 306 |
K_L = keyL deprecated; |
| 307 |
K_M = keyM deprecated; |
| 308 |
K_N = keyN deprecated; |
| 309 |
K_O = keyO deprecated; |
| 310 |
K_P = keyP deprecated; |
| 311 |
K_Q = keyQ deprecated; |
| 312 |
K_R = keyR deprecated; |
| 313 |
K_S = keyS deprecated; |
| 314 |
K_T = keyT deprecated; |
| 315 |
K_U = keyU deprecated; |
| 316 |
K_V = keyV deprecated; |
| 317 |
K_W = keyW deprecated; |
| 318 |
K_X = keyX deprecated; |
| 319 |
K_Y = keyY deprecated; |
| 320 |
K_Z = keyZ deprecated; |
| 321 |
K_LeftBracket = keyLeftBracket deprecated; |
| 322 |
K_RightBracket = keyRightBracket deprecated; |
| 323 |
K_Numpad_Plus = keyNumpadPlus deprecated; |
| 324 |
K_Numpad_Minus = keyNumpadMinus deprecated; |
| 325 |
K_F1 = keyF1 deprecated; |
| 326 |
K_F2 = keyF2 deprecated; |
| 327 |
K_F3 = keyF3 deprecated; |
| 328 |
K_F4 = keyF4 deprecated; |
| 329 |
K_F5 = keyF5 deprecated; |
| 330 |
K_F6 = keyF6 deprecated; |
| 331 |
K_F7 = keyF7 deprecated; |
| 332 |
K_F8 = keyF8 deprecated; |
| 333 |
K_F9 = keyF9 deprecated; |
| 334 |
K_F10 = keyF10 deprecated; |
| 335 |
K_F11 = keyF11 deprecated; |
| 336 |
K_F12 = keyF12 deprecated; |
| 337 |
K_Numpad_0 = keyNumpad0 deprecated; |
| 338 |
K_Numpad_1 = keyNumpad1 deprecated; |
| 339 |
K_Numpad_2 = keyNumpad2 deprecated; |
| 340 |
K_Numpad_3 = keyNumpad3 deprecated; |
| 341 |
K_Numpad_4 = keyNumpad4 deprecated; |
| 342 |
K_Numpad_5 = keyNumpad5 deprecated; |
| 343 |
K_Numpad_6 = keyNumpad6 deprecated; |
| 344 |
K_Numpad_7 = keyNumpad7 deprecated; |
| 345 |
K_Numpad_8 = keyNumpad8 deprecated; |
| 346 |
K_Numpad_9 = keyNumpad9 deprecated; |
| 347 |
K_Numpad_End = keyNumpadEnd deprecated; |
| 348 |
K_Numpad_Down = keyNumpadDown deprecated; |
| 349 |
K_Numpad_PageDown = keyNumpadPageDown deprecated; |
| 350 |
K_Numpad_Left = keyNumpadLeft deprecated; |
| 351 |
K_Numpad_Begin = keyNumpadBegin deprecated; |
| 352 |
K_Numpad_Right = keyNumpadRight deprecated; |
| 353 |
K_Numpad_Home = keyNumpadHome deprecated; |
| 354 |
K_Numpad_Up = keyNumpadUp deprecated; |
| 355 |
K_Numpad_PageUp = keyNumpadPageUp deprecated; |
| 356 |
K_Numpad_Insert = keyNumpadInsert deprecated; |
| 357 |
K_Numpad_Delete = keyNumpadDelete deprecated; |
| 358 |
K_Numpad_Enter = keyNumpadEnter deprecated; |
| 359 |
K_Numpad_Multiply = keyNumpadMultiply deprecated; |
| 360 |
K_Numpad_Divide = keyNumpadDivide deprecated; |
| 361 |
K_Comma = keyComma deprecated; |
| 362 |
K_Period = keyPeriod deprecated; |
| 363 |
|
| 364 |
keyLeft = keyArrowLeft deprecated 'Use keyArrowLeft'; |
| 365 |
keyUp = keyArrowUp deprecated 'Use keyArrowUp'; |
| 366 |
keyRight = keyArrowRight deprecated 'Use keyArrowRight'; |
| 367 |
keyDown = keyArrowDown deprecated 'Use keyArrowDown'; |
| 368 |
|
| 369 |
type |
| 370 |
TKeysBooleans = array [TKey] of Boolean; |
| 371 |
PKeysBooleans = ^TKeysBooleans; |
| 372 |
TKeysBytes = array [Byte] of TKey; |
| 373 |
PKeysBytes = ^TKeysBytes; |
| 374 |
|
| 375 |
TCharactersBooleans = array [Char] of Boolean; |
| 376 |
PCharactersBooleans = ^TCharactersBooleans; |
| 377 |
|
| 378 |
TCastleMouseButton = (buttonLeft, buttonMiddle, buttonRight, buttonExtra1, buttonExtra2); |
| 379 |
TCastleMouseButtons = set of TCastleMouseButton; |
| 380 |
|
| 381 |
type |
| 382 |
{ Look of the mouse cursor. |
| 383 |
Used for various properties: |
| 384 |
TCastleUserInterface.Cursor, TCastleTransform.Cursor, TCastleWindow.Cursor. |
| 385 |
|
| 386 |
mcDefault, mcNone, mcForceNone have somewhat special meanings. |
| 387 |
The rest are some cursor images will well-defined meanings for the user, |
| 388 |
their exact look may depend on current window manager theme etc. } |
| 389 |
TMouseCursor = ( |
| 390 |
{ Leave cursor as default, decided by a parent UI control, or by window manager. } |
| 391 |
mcDefault, |
| 392 |
{ Make cursor invisible. } |
| 393 |
mcNone, |
| 394 |
{ Forcefully make cursor invisible. |
| 395 |
|
| 396 |
If *any* UI control under the cursor |
| 397 |
says that the cursor is mcForceNone, it will be invisible. |
| 398 |
This is in contrast to mcNone, that only hides the cursor if |
| 399 |
the currently focused control (under the mouse cursor) sets it. } |
| 400 |
mcForceNone, |
| 401 |
{ Standard arrow, indicates that user can point / click something. } |
| 402 |
mcStandard, |
| 403 |
{ Indicates the program is busy and user should wait. } |
| 404 |
mcWait, |
| 405 |
{ Text cursor, indicates that there's text under the cursor, |
| 406 |
which usually means that it can be selected, |
| 407 |
or that user can click to set focus to the text area. } |
| 408 |
mcText, |
| 409 |
{ Indicates something active is under cursor, usually for links. } |
| 410 |
mcHand, |
| 411 |
|
| 412 |
mcResizeVertical, |
| 413 |
mcResizeHorizontal, |
| 414 |
mcResizeTopLeft, |
| 415 |
mcResizeTop, |
| 416 |
mcResizeTopRight, |
| 417 |
mcResizeLeft, |
| 418 |
mcResizeRight, |
| 419 |
mcResizeBottomLeft, |
| 420 |
mcResizeBottom, |
| 421 |
mcResizeBottomRight |
| 422 |
); |
| 423 |
const |
| 424 |
MouseButtonStr: array [TCastleMouseButton] of string = ( |
| 425 |
'left', 'middle', 'right', 'extra1', 'extra2'); |
| 426 |
|
| 427 |
type |
| 428 |
{ Modifier keys are keys that, when pressed, modify the meaning of |
| 429 |
other keys. Of course, this is actually just a convention. |
| 430 |
The actual interpretation is left up to the final program |
| 431 |
-- there you have to decide when and how modifiers affect the |
| 432 |
meaning of other keys. } |
| 433 |
TModifierKey = (mkCtrl, mkShift, mkAlt); |
| 434 |
TModifierKeys = set of TModifierKey; |
| 435 |
|
| 436 |
{ Tracking the "pressed" state of keys. Allows you to query is key (TKey) |
| 437 |
pressed, and is some character (Char type) pressed. } |
| 438 |
TKeysPressed = class |
| 439 |
private |
| 440 |
{ Characters are updated on the basis that given |
| 441 |
TKey corresponds to character. PressedKeyToCharacter and |
| 442 |
PressedCharacterToKey arrays |
| 443 |
store 1-1 mapping between pressed keys and pressed characters. |
| 444 |
So at any given point, we consider that given character corresponds |
| 445 |
to only one key. So Characters may get fooled by user in some |
| 446 |
complicated cases, that's acceptable (since unavoidable, |
| 447 |
see Characters comments). If a new key corresponding to already |
| 448 |
pressed character is pressed, this new key replaces previous key |
| 449 |
in the mapping. |
| 450 |
|
| 451 |
PressedKeyToCharacter reverse each other, that is |
| 452 |
PressedCharacterToKey[PressedKeyToCharacter[Key]] = Key and |
| 453 |
PressedKeyToCharacter[PressedCharacterToKey[C]] = C |
| 454 |
for all keys and characters, assuming that |
| 455 |
PressedCharacterToKey[C] <> keyNone and |
| 456 |
PressedKeyToCharacter[Key] <> #0 (which indicate that no character |
| 457 |
is pressed / no character is pressed corresponding to this key). |
| 458 |
|
| 459 |
Storing correspondence means that if each KeyDown is paired by |
| 460 |
KeyUp, then each pressed Character will also be released. |
| 461 |
(Since each pressed Character *always* has a corresponding key |
| 462 |
that activated it.) |
| 463 |
|
| 464 |
PressedXxx arrays may seem complicated, but their programming |
| 465 |
is trivial and they allow me to update Characters array |
| 466 |
quickly and reliably. } |
| 467 |
PressedKeyToCharacter: array [TKey] of Char; |
| 468 |
PressedCharacterToKey: array [Char] of TKey; |
| 469 |
|
| 470 |
function GetItems(const Key: TKey): Boolean; |
| 471 |
function GetStrings(const KeyString: String): Boolean; |
| 472 |
public |
| 473 |
{ Check is a key (TKey) pressed. |
| 474 |
|
| 475 |
This array is read-only from outside of this class! |
| 476 |
Always Keys[keyNone] = false. } |
| 477 |
Keys: TKeysBooleans; |
| 478 |
|
| 479 |
{ Check is a character pressed. |
| 480 |
|
| 481 |
This array is read-only from outside of this class! |
| 482 |
Always Characters[#0] = false. |
| 483 |
|
| 484 |
Note that since a given character may be generated by various |
| 485 |
key combinations, this doesn't work as reliably as @link(Keys) array. |
| 486 |
For example, consider pressing a, then shift, then releasing a |
| 487 |
--- for a short time character 'A', and not 'a', should be pressed. |
| 488 |
|
| 489 |
Although we do our best (have some mapping tables to track characters), |
| 490 |
and in practice this works Ok. But still checking for keys |
| 491 |
on @link(Keys) array, when possible, is advised. } |
| 492 |
Characters: TCharactersBooleans; |
| 493 |
|
| 494 |
{ Check is a key (TKey) pressed. |
| 495 |
Returns the same values as are in the @link(Keys) table. |
| 496 |
It's a default property of this class, |
| 497 |
so you can write e.g. |
| 498 |
@code(Window.Pressed[keyX]) instead of |
| 499 |
@code(Window.Pressed.Keys[keyX]). } |
| 500 |
property Items [const Key: TKey]: boolean read GetItems; default; |
| 501 |
|
| 502 |
{ Does an UTF-8 key represented by this String is pressed. |
| 503 |
Note that internallly we only track 8-bit keys (Char) for now, |
| 504 |
but this will change some day. } |
| 505 |
property Strings [const KeyString: String]: Boolean read GetStrings; |
| 506 |
|
| 507 |
{ Check which modifier keys are pressed. |
| 508 |
The result it based on current Keys[keyCtrl], Keys[keyShift] etc. values. } |
| 509 |
function Modifiers: TModifierKeys; |
| 510 |
|
| 511 |
{ Call when key is pressed. |
| 512 |
Pass TKey, and corresponding character (as String, since it may be UTF-8 character). |
| 513 |
|
| 514 |
Pass Key = keyNone if this is not representable as TKey, |
| 515 |
pass KeyString = '' if this is not representable as String. |
| 516 |
But never pass both Key = keyNone and KeyString = '' |
| 517 |
(this would mean that nothing was pressed, as least nothing that can be |
| 518 |
represented by CGE). } |
| 519 |
procedure KeyDown(const Key: TKey; const KeyString: String); |
| 520 |
|
| 521 |
{ Call when key is released. |
| 522 |
Never pass Key = keyNone here. |
| 523 |
|
| 524 |
It returns which character was released as a consequence of this |
| 525 |
key release. } |
| 526 |
procedure KeyUp(const Key: TKey; out KeyString: String); |
| 527 |
|
| 528 |
{ Mark all keys as released. That is, this sets all @link(Keys) and |
| 529 |
@link(Characters) items to @false. Also resets internal arrays |
| 530 |
used to track @link(Characters) from KeyDown and KeyUp. } |
| 531 |
procedure Clear; |
| 532 |
end; |
| 533 |
|
| 534 |
function KeyToStr(const Key: TKey; const Modifiers: TModifierKeys = []; |
| 535 |
const CtrlIsCommand: boolean = false): string; |
| 536 |
|
| 537 |
const |
| 538 |
ModifierKeyToKey: array[TModifierKey]of TKey = (keyCtrl, keyShift, keyAlt); |
| 539 |
|
| 540 |
{ Determine pressed modifier keys (ctrl, shift and so on). |
| 541 |
|
| 542 |
Overloaded version with TKeysPressed parameter allows the parameter to be @nil, |
| 543 |
and returns [] (empty set) then. |
| 544 |
|
| 545 |
@groupBegin } |
| 546 |
function ModifiersDown(const KeysDown: TKeysBooleans): TModifierKeys; overload; |
| 547 |
function ModifiersDown(const Pressed: TKeysPressed): TModifierKeys; overload; |
| 548 |
{ @groupEnd } |
| 549 |
|
| 550 |
function ModifierKeysToNiceStr(const MK: TModifierKeys): string; |
| 551 |
|
| 552 |
{ Nice short description of the character. |
| 553 |
When Modifiers is not empty, these are the additional modifiers |
| 554 |
required to be pressed (although some C values, like CtrlA ... CtrlZ, |
| 555 |
may already indicate some modifier). |
| 556 |
|
| 557 |
For normal readable characters just returns them, for special |
| 558 |
characters returns short string like "Ctrl+C" or "Escape". |
| 559 |
|
| 560 |
The returned string doesn't contain any quotes around, doesn't |
| 561 |
contain any word merely stating "character" (for example argument 'c' just |
| 562 |
generates 'c', not 'character "c"'). |
| 563 |
|
| 564 |
BackSpaceTabEnterString determines behavior on three special values: |
| 565 |
#8, #9, #13. These may be either described as Backspace/Tab/Enter |
| 566 |
(if BackSpaceTabEnterString = true) |
| 567 |
or as Ctrl+H, Ctrl+I, Ctrl+M (if BackSpaceTabEnterString = false). } |
| 568 |
function CharToNiceStr(const C: char; const Modifiers: TModifierKeys = []; |
| 569 |
const BackSpaceTabEnterString: boolean = true; |
| 570 |
const CtrlIsCommand: boolean = false): string; |
| 571 |
|
| 572 |
{ Like @link(CharToNiceStr), but accepts UTF-8 characters expressed as String. |
| 573 |
KeyString = '' means "none". } |
| 574 |
function KeyStringToNiceStr(const KeyString: String; |
| 575 |
const Modifiers: TModifierKeys = []; |
| 576 |
const BackSpaceTabEnterString: boolean = true; |
| 577 |
const CtrlIsCommand: boolean = false): string; |
| 578 |
|
| 579 |
type |
| 580 |
TMouseWheelDirection = (mwNone, mwUp, mwDown, mwLeft, mwRight); |
| 581 |
|
| 582 |
const |
| 583 |
MouseWheelDirectionStr: array [TMouseWheelDirection] of string = |
| 584 |
('none', 'up', 'down', 'left', 'right'); |
| 585 |
|
| 586 |
{ Determine simple mouse wheel direction from a Scroll and Vertical |
| 587 |
parameters received from TCastleWindow.OnMouseWheel. |
| 588 |
Assumes that Scroll <> 0, like TCastleWindow.OnMouseWheel guarantees. } |
| 589 |
function MouseWheelDirection(const Scroll: Single; const Vertical: boolean): TMouseWheelDirection; |
| 590 |
|
| 591 |
{ Convert string value back to a key name, reversing KeyToStr. |
| 592 |
If string does not contain any recognized key name, return DefaultKey. } |
| 593 |
function StrToKey(const S: string; const DefaultKey: TKey): TKey; |
| 594 |
|
| 595 |
type |
| 596 |
TInputPressReleaseType = (itKey, itMouseButton, itMouseWheel); |
| 597 |
|
| 598 |
TFingerIndex = Cardinal; |
| 599 |
|
| 600 |
{ Input press or release event. |
| 601 |
Either key press/release or mouse button press/release or |
| 602 |
mouse wheel action. |
| 603 |
This is nicely matching with TInputShortcut processing in CastleInputs, |
| 604 |
so it allows to easily store and check for user actions. } |
| 605 |
TInputPressRelease = record |
| 606 |
EventType: TInputPressReleaseType; |
| 607 |
|
| 608 |
{ When EventType is itKey, this is the key pressed or released. |
| 609 |
Either Key <> keyNone or KeyString <> '' in this case. |
| 610 |
When EventType <> itKey, then Key = keyNone and KeyString = ''. |
| 611 |
|
| 612 |
Both Key and KeyString represent the same action. Sometimes one, |
| 613 |
sometimes the other is useful. |
| 614 |
|
| 615 |
@bold(Not all key presses can be represented as TKey value.) |
| 616 |
For example, pressing '(' (opening parenthesis), which is done on most |
| 617 |
keyboards by pressing shift + zero, does not have any TKey value. |
| 618 |
So it will generate event with Key = keyNone, but KeyString = '('. |
| 619 |
|
| 620 |
@bold(Likewise, not all key presses can be represented as UTF8 char or |
| 621 |
simple char.) For example "up arrow" (Key = keyUp) doesn't have a char code |
| 622 |
(it will have KeyString = '' and KeyCharacter = #0). |
| 623 |
|
| 624 |
KeyString is a string (encoded using UTF-8, like all strings |
| 625 |
in Castle Game Engine) and is influenced by some other keys state, |
| 626 |
like Shift or Ctrl or CapsLock or some key to input localized characters |
| 627 |
(all dependent on your system settings, we don't deal with it in our engine, |
| 628 |
we merely take what system gives us). For example, you can get "a" or "A" |
| 629 |
depending of Shift and CapsLock state, or CtrlA if you hold Ctrl. |
| 630 |
|
| 631 |
When the user holds the key pressed, we will get consecutive |
| 632 |
key down events. Under some OSes, you will also get consecutive |
| 633 |
key up events, but it's not guaranteed (on some OSes, you may |
| 634 |
simply get only consecutive key down). So the more precise |
| 635 |
definition when key down occurs is: it's a notification that |
| 636 |
the key is (still) pressed down. |
| 637 |
@groupBegin } |
| 638 |
Key: TKey; |
| 639 |
KeyString: String; |
| 640 |
{ @groupEnd } |
| 641 |
|
| 642 |
{ ModifiersDown contains a set of modifier keys (i.e. Ctrl, Shift and Alt) |
| 643 |
which were pressed at the moment of this Event. } |
| 644 |
ModifiersDown: TModifierKeys; |
| 645 |
|
| 646 |
{ Was this key already pressed before this event. |
| 647 |
May be @true only for key events, and only on press (not on release). |
| 648 |
Alllows to recognize "key repeat" that occurs when you press a key |
| 649 |
for some time. The keyboard generates "key down" events then |
| 650 |
(with delay and frequency depending on keyboard settings). |
| 651 |
Sometimes they are useful, sometimes not -- so you can recognize |
| 652 |
them using this field. } |
| 653 |
KeyRepeated: boolean; |
| 654 |
|
| 655 |
{ When EventType is itMouseButton, this is the mouse button pressed or released. |
| 656 |
Always buttonLeft for touch device press/release events. |
| 657 |
|
| 658 |
CastleWindow notes (but relevant also to other interfaces, like Lazarus |
| 659 |
component, although in that case it's beyond our control): |
| 660 |
When user presses the mouse over |
| 661 |
our control, mouse is automatically captured, so all further OnMotion |
| 662 |
following mouse release will be passed to this control (even if user moves mouse |
| 663 |
outside of this control), until user releases all mouse buttons. |
| 664 |
Note that this means that mouse positions may be outside |
| 665 |
of [0..Width - 1, 0..Height - 1] range. } |
| 666 |
MouseButton: TCastleMouseButton; |
| 667 |
|
| 668 |
{ When EventType is itMouseButton, this is the finger index pressed or |
| 669 |
released on a touch device. Always 0 for normal mouse events. } |
| 670 |
FingerIndex: TFingerIndex; |
| 671 |
|
| 672 |
{ The position of the current mouse/finger on the container, |
| 673 |
for EventType = itMouseButton (in case of mouse press/release). |
| 674 |
|
| 675 |
The position is relative to the whole container (rendering area |
| 676 |
of TCastleWindow or TCastleControl). With left-bottom being |
| 677 |
(0,0) and X growing to the right and Y growing up. |
| 678 |
The position is in final device coordinates, i.e. it ignores |
| 679 |
@link(TCastleContainer.UIScaling). |
| 680 |
Use e.g. @link(TCastleUserInterface.ContainerToLocalPosition) |
| 681 |
to easily convert this position into a position suitable for given UI control |
| 682 |
children. |
| 683 |
|
| 684 |
For normal backends that simply support a single mouse device, |
| 685 |
this is just equivalent to TCastleWindow.MousePosition |
| 686 |
and TCastleControl.MousePosition. |
| 687 |
|
| 688 |
For multi-touch devices, this describes |
| 689 |
the position of the current finger (corresponding to FingerIndex). |
| 690 |
|
| 691 |
For other EventType values (not itMouseButton), |
| 692 |
this is the position of main mouse/finger. |
| 693 |
See TCastleWindow.MousePosition documentation for what it means, |
| 694 |
in particular what happens on touch devices. } |
| 695 |
Position: TVector2; |
| 696 |
|
| 697 |
{ When EventType is itMouseWheel, this is the mouse wheel action. |
| 698 |
MouseWheel is mwNone if and only if EventType <> itMouseWheel. |
| 699 |
|
| 700 |
Positive value of Scroll means user scrolled up or left, |
| 701 |
negative means user scrolled down or right. It is never zero |
| 702 |
(as long as EventType = itMouseWheel of course). |
| 703 |
|
| 704 |
Scroll units are such that 1.0 should be treated like a "one operation", |
| 705 |
like a one click. On most normal mouses only an integer scroll will be |
| 706 |
possible to make. On the other hand, on touchpads it's common to be able |
| 707 |
to scroll by flexible amounts. |
| 708 |
|
| 709 |
CastleWindow backends notes: |
| 710 |
GTK and Xlib cannot generate Scroll values different than 1 or -1. |
| 711 |
|
| 712 |
@groupBegin } |
| 713 |
MouseWheelScroll: Single; |
| 714 |
MouseWheelVertical: boolean; |
| 715 |
function MouseWheel: TMouseWheelDirection; |
| 716 |
{ @groupEnd } |
| 717 |
|
| 718 |
{ Does the event indicate pressing the given key. |
| 719 |
This overloaded version, without RequiredModifiers parameter, |
| 720 |
ignores the currently pressed modifiers. |
| 721 |
Always @false when AKey is keyNone. } |
| 722 |
function IsKey(const AKey: TKey): Boolean; overload; |
| 723 |
|
| 724 |
{ Does the event indicate pressing the given key. |
| 725 |
This overloaded version, with RequiredModifiers, checks that pressed |
| 726 |
modifiers match exactly the RequiredModifiers. |
| 727 |
Always @false when AKey is keyNone. } |
| 728 |
function IsKey(const AKey: TKey; const RequiredModifiers: TModifierKeys): Boolean; overload; |
| 729 |
|
| 730 |
{ Does the event indicate pressing the given key, with key given as a |
| 731 |
string representation. |
| 732 |
The AKeyString is a single Unicode character, represented as a String |
| 733 |
(UTF-8 string with FPC, UTF-16 string with Delphi). |
| 734 |
Always @false when AKeyString is ''. } |
| 735 |
function IsKey(AKeyString: String): Boolean; overload; |
| 736 |
|
| 737 |
function IsMouseButton(const AMouseButton: TCastleMouseButton): Boolean; |
| 738 |
function IsMouseWheel(const AMouseWheel: TMouseWheelDirection): Boolean; |
| 739 |
|
| 740 |
{ Textual description of this event. } |
| 741 |
function ToString: string; |
| 742 |
{ Character corresponding to EventType = itKey. |
| 743 |
Returns #0 if the event was not a keyboard event or it cannot be |
| 744 |
represented as a simple 8-bit character (e.g. it is a Cyrillic or Arabic |
| 745 |
character, or it is a special key like "up arrow"). } |
| 746 |
function KeyCharacter: char; |
| 747 |
{ @deprecated Deprecated name for ToString. } |
| 748 |
function Description: string; deprecated; |
| 749 |
end; |
| 750 |
|
| 751 |
{ Motion (movement) of mouse or a finger on a touch device. } |
| 752 |
TInputMotion = record |
| 753 |
{ Old and new positions of the mouse or finger. |
| 754 |
In the same coordinate system as @link(TInputPressRelease.Position). } |
| 755 |
OldPosition, Position: TVector2; |
| 756 |
|
| 757 |
{ Currently pressed mouse buttons. |
| 758 |
On touch devices, this is always just [buttonLeft]. } |
| 759 |
Pressed: TCastleMouseButtons; |
| 760 |
|
| 761 |
{ Finger that is moving, on touch devices. |
| 762 |
If you use mouse, this is always just 0. } |
| 763 |
FingerIndex: TFingerIndex; |
| 764 |
end; |
| 765 |
|
| 766 |
{ Construct TInputPressRelease corresponding to given event. |
| 767 |
@groupBegin } |
| 768 |
function InputKey(const Position: TVector2; const Key: TKey; |
| 769 |
const KeyString: string; |
| 770 |
const ModifiersDown: TModifierKeys = []): TInputPressRelease; |
| 771 |
function InputMouseButton(const Position: TVector2; |
| 772 |
const MouseButton: TCastleMouseButton; const FingerIndex: TFingerIndex; |
| 773 |
const ModifiersDown: TModifierKeys = []): TInputPressRelease; |
| 774 |
function InputMouseWheel(const Position: TVector2; |
| 775 |
const Scroll: Single; const Vertical: Boolean; |
| 776 |
const ModifiersDown: TModifierKeys = []): TInputPressRelease; |
| 777 |
{ @groupEnd } |
| 778 |
|
| 779 |
{ Construct TInputMotion. } |
| 780 |
function InputMotion(const OldPosition, Position: TVector2; |
| 781 |
const Pressed: TCastleMouseButtons; const FingerIndex: TFingerIndex): TInputMotion; |
| 782 |
|
| 783 |
type |
| 784 |
TCastleConfigKeysMouseHelper = class helper for TCastleConfig |
| 785 |
{ Reading/writing key values to config file. |
| 786 |
Key names are expected to follow StrToKey and KeyToStr functions in CastleKeysMouse. |
| 787 |
|
| 788 |
@groupBegin } |
| 789 |
function GetKey(const APath: string; |
| 790 |
const ADefaultValue: TKey): TKey; overload; |
| 791 |
procedure SetKey(const APath: string; |
| 792 |
const AValue: TKey); overload; |
| 793 |
procedure SetDeleteKey(const APath: string; |
| 794 |
const AValue, ADefaultValue: TKey); overload; |
| 795 |
{ @groupEnd } |
| 796 |
end; |
| 797 |
|
| 798 |
implementation |
| 799 |
|
| 800 |
uses SysUtils, Math; |
| 801 |
|
| 802 |
const |
| 803 |
KeyToStrTable: array [TKey] of string = ( |
| 804 |
'None', |
| 805 |
'Print Screen', |
| 806 |
'Caps Lock', |
| 807 |
'Scroll Lock', |
| 808 |
'Num Lock', |
| 809 |
'Pause', |
| 810 |
'Apostrophe', |
| 811 |
'Semicolon', |
| 812 |
'BackSpace', |
| 813 |
'Tab', |
| 814 |
'Slash', |
| 815 |
'BackQuote', |
| 816 |
'Minus', |
| 817 |
'Enter', |
| 818 |
'Equal', |
| 819 |
'BackSlash', |
| 820 |
'Shift', |
| 821 |
'Ctrl', |
| 822 |
'Alt', |
| 823 |
'Plus', |
| 824 |
'Reserved_20', |
| 825 |
'Reserved_21', |
| 826 |
'Reserved_22', |
| 827 |
'Reserved_23', |
| 828 |
'Reserved_24', |
| 829 |
'Reserved_25', |
| 830 |
'Reserved_26', |
| 831 |
'Escape', |
| 832 |
'Reserved_28', |
| 833 |
'Reserved_29', |
| 834 |
'Reserved_30', |
| 835 |
'Reserved_31', |
| 836 |
'Space', |
| 837 |
'Page Up', |
| 838 |
'Page Down', |
| 839 |
'End', |
| 840 |
'Home', |
| 841 |
'Left', |
| 842 |
'Up', |
| 843 |
'Right', |
| 844 |
'Down', |
| 845 |
'Reserved_41', |
| 846 |
'Reserved_42', |
| 847 |
'Reserved_43', |
| 848 |
'Reserved_44', |
| 849 |
'Insert', |
| 850 |
'Delete', |
| 851 |
'Reserved_47', |
| 852 |
'0', |
| 853 |
'1', |
| 854 |
'2', |
| 855 |
'3', |
| 856 |
'4', |
| 857 |
'5', |
| 858 |
'6', |
| 859 |
'7', |
| 860 |
'8', |
| 861 |
'9', |
| 862 |
'Reserved_58', |
| 863 |
'Reserved_59', |
| 864 |
'Reserved_60', |
| 865 |
'Reserved_61', |
| 866 |
'Reserved_62', |
| 867 |
'Reserved_63', |
| 868 |
'Reserved_64', |
| 869 |
'A', |
| 870 |
'B', |
| 871 |
'C', |
| 872 |
'D', |
| 873 |
'E', |
| 874 |
'F', |
| 875 |
'G', |
| 876 |
'H', |
| 877 |
'I', |
| 878 |
'J', |
| 879 |
'K', |
| 880 |
'L', |
| 881 |
'M', |
| 882 |
'N', |
| 883 |
'O', |
| 884 |
'P', |
| 885 |
'Q', |
| 886 |
'R', |
| 887 |
'S', |
| 888 |
'T', |
| 889 |
'U', |
| 890 |
'V', |
| 891 |
'W', |
| 892 |
'X', |
| 893 |
'Y', |
| 894 |
'Z', |
| 895 |
'[', |
| 896 |
'Reserved_92', |
| 897 |
']', |
| 898 |
'Reserved_94', |
| 899 |
'Reserved_95', |
| 900 |
'Reserved_96', |
| 901 |
'Reserved_97', |
| 902 |
'Reserved_98', |
| 903 |
'Reserved_99', |
| 904 |
'Reserved_100', |
| 905 |
'Reserved_101', |
| 906 |
'Reserved_102', |
| 907 |
'Reserved_103', |
| 908 |
'Reserved_104', |
| 909 |
'Reserved_105', |
| 910 |
'Reserved_106', |
| 911 |
'Numpad Plus', |
| 912 |
'Reserved_108', |
| 913 |
'Numpad Minus', |
| 914 |
'Reserved_110', |
| 915 |
'Reserved_111', |
| 916 |
'F1', |
| 917 |
'F2', |
| 918 |
'F3', |
| 919 |
'F4', |
| 920 |
'F5', |
| 921 |
'F6', |
| 922 |
'F7', |
| 923 |
'F8', |
| 924 |
'F9', |
| 925 |
'F10', |
| 926 |
'F11', |
| 927 |
'F12', |
| 928 |
'Reserved_124', |
| 929 |
'Reserved_125', |
| 930 |
'Reserved_126', |
| 931 |
'Reserved_127', |
| 932 |
'Reserved_128', |
| 933 |
'Reserved_129', |
| 934 |
'Reserved_130', |
| 935 |
'Reserved_131', |
| 936 |
'Reserved_132', |
| 937 |
'Reserved_133', |
| 938 |
'Reserved_134', |
| 939 |
'Reserved_135', |
| 940 |
'Reserved_136', |
| 941 |
'Reserved_137', |
| 942 |
'Reserved_138', |
| 943 |
'Reserved_139', |
| 944 |
'Numpad 0', |
| 945 |
'Numpad 1', |
| 946 |
'Numpad 2', |
| 947 |
'Numpad 3', |
| 948 |
'Numpad 4', |
| 949 |
'Numpad 5', |
| 950 |
'Numpad 6', |
| 951 |
'Numpad 7', |
| 952 |
'Numpad 8', |
| 953 |
'Numpad 9', |
| 954 |
'Numpad End', |
| 955 |
'Numpad Down', |
| 956 |
'Numpad PageDown', |
| 957 |
'Numpad Left', |
| 958 |
'Numpad Begin', |
| 959 |
'Numpad Right', |
| 960 |
'Numpad Home', |
| 961 |
'Numpad Up', |
| 962 |
'Numpad PageUp', |
| 963 |
'Numpad Insert', |
| 964 |
'Numpad Delete', |
| 965 |
'Numpad Enter', |
| 966 |
'Numpad Multiply', |
| 967 |
'Numpad Divide', |
| 968 |
'Pad A', |
| 969 |
'Pad B', |
| 970 |
'Pad X', |
| 971 |
'Pad Y', |
| 972 |
'Pad L', |
| 973 |
'Pad R', |
| 974 |
'Pad ZL', |
| 975 |
'Pad ZR', |
| 976 |
'Pad Plus', |
| 977 |
'Pad Minus', |
| 978 |
'Pad Left', |
| 979 |
'Pad Up', |
| 980 |
'Pad Right', |
| 981 |
'Pad Down', |
| 982 |
'Reserved_178', |
| 983 |
'Reserved_179', |
| 984 |
'Reserved_180', |
| 985 |
'Reserved_181', |
| 986 |
'Reserved_182', |
| 987 |
'Reserved_183', |
| 988 |
'Reserved_184', |
| 989 |
'Reserved_185', |
| 990 |
'Reserved_186', |
| 991 |
'Reserved_187', |
| 992 |
'Comma', |
| 993 |
'Reserved_189', |
| 994 |
'Period', |
| 995 |
'Reserved_191' |
| 996 |
); |
| 997 |
|
| 998 |
function KeyToStr(const Key: TKey; const Modifiers: TModifierKeys; |
| 999 |
const CtrlIsCommand: boolean): string; |
| 1000 |
begin |
| 1001 |
{ early exit, key keyNone means "no key", Modifiers are ignored } |
| 1002 |
if Key = keyNone then Exit(KeyToStrTable[Key]); |
| 1003 |
|
| 1004 |
Result := ''; |
| 1005 |
|
| 1006 |
{ add modifiers description } |
| 1007 |
if mkShift in Modifiers then |
| 1008 |
Result := Result + 'Shift+'; |
| 1009 |
if mkAlt in Modifiers then |
| 1010 |
Result := Result + 'Alt+'; |
| 1011 |
if mkCtrl in Modifiers then |
| 1012 |
begin |
| 1013 |
if CtrlIsCommand then |
| 1014 |
Result := Result + 'Command+' |
| 1015 |
else |
| 1016 |
Result := Result + 'Ctrl+'; |
| 1017 |
end; |
| 1018 |
|
| 1019 |
Result := Result + KeyToStrTable[Key]; |
| 1020 |
end; |
| 1021 |
|
| 1022 |
function StrToKey(const S: string; const DefaultKey: TKey): TKey; |
| 1023 |
begin |
| 1024 |
for Result := Low(Result) to High(Result) do |
| 1025 |
if KeyToStrTable[Result] = S then |
| 1026 |
Exit; |
| 1027 |
Result := DefaultKey; |
| 1028 |
end; |
| 1029 |
|
| 1030 |
function ModifiersDown(const KeysDown: TKeysBooleans): TModifierKeys; |
| 1031 |
var |
| 1032 |
mk: TModifierKey; |
| 1033 |
begin |
| 1034 |
Result := []; |
| 1035 |
for mk := Low(TModifierKey) to High(TModifierKey) do |
| 1036 |
if KeysDown[ModifierKeyToKey[mk]] then |
| 1037 |
Include(Result, mk); |
| 1038 |
end; |
| 1039 |
|
| 1040 |
function ModifiersDown(const Pressed: TKeysPressed): TModifierKeys; |
| 1041 |
begin |
| 1042 |
if Pressed <> nil then |
| 1043 |
Result := ModifiersDown(Pressed.Keys) |
| 1044 |
else |
| 1045 |
Result := []; |
| 1046 |
end; |
| 1047 |
|
| 1048 |
function ModifierKeysToNiceStr(const MK: TModifierKeys): string; |
| 1049 |
var |
| 1050 |
K: TModifierKey; |
| 1051 |
begin |
| 1052 |
Result := ''; |
| 1053 |
for K in MK do |
| 1054 |
Result := SAppendPart(Result, ', ', KeyToStr(ModifierKeyToKey[K])); |
| 1055 |
Result := '[' + Result + ']'; |
| 1056 |
end; |
| 1057 |
|
| 1058 |
function KeyStringToNiceStr(const KeyString: String; |
| 1059 |
const Modifiers: TModifierKeys = []; |
| 1060 |
const BackSpaceTabEnterString: boolean = true; |
| 1061 |
const CtrlIsCommand: boolean = false): string; |
| 1062 |
begin |
| 1063 |
case Length(KeyString) of |
| 1064 |
0: Result := 'none'; |
| 1065 |
1: Result := CharToNiceStr(KeyString[1], Modifiers, BackSpaceTabEnterString, CtrlIsCommand); |
| 1066 |
else Result := KeyString; // UTF-8 multi-byte char, just show it |
| 1067 |
end; |
| 1068 |
end; |
| 1069 |
|
| 1070 |
function CharToNiceStr(const C: char; const Modifiers: TModifierKeys; |
| 1071 |
const BackSpaceTabEnterString, CtrlIsCommand: boolean): string; |
| 1072 |
var |
| 1073 |
CharactersImplicatingCtrlModifier: TSetOfChars; |
| 1074 |
begin |
| 1075 |
{ early exit, character #0 means "no key", Modifiers are ignored } |
| 1076 |
if C = #0 then Exit('#0'); |
| 1077 |
|
| 1078 |
Result := ''; |
| 1079 |
|
| 1080 |
CharactersImplicatingCtrlModifier := [CtrlA .. CtrlZ]; |
| 1081 |
if BackSpaceTabEnterString then |
| 1082 |
{ do not show Tab and similar chars as Ctrl+Tab } |
| 1083 |
CharactersImplicatingCtrlModifier := CharactersImplicatingCtrlModifier - |
| 1084 |
[CharBackSpace, CharTab, CharEnter]; |
| 1085 |
|
| 1086 |
{ add modifiers description } |
| 1087 |
if (mkShift in Modifiers) or CharInSet(C, ['A'..'Z']) then |
| 1088 |
Result := Result + 'Shift+'; |
| 1089 |
if mkAlt in Modifiers then |
| 1090 |
Result := Result + 'Alt+'; |
| 1091 |
if (mkCtrl in Modifiers) or |
| 1092 |
CharInSet(C, CharactersImplicatingCtrlModifier) then |
| 1093 |
begin |
| 1094 |
if CtrlIsCommand then |
| 1095 |
Result := Result + 'Command+' |
| 1096 |
else |
| 1097 |
Result := Result + 'Ctrl+'; |
| 1098 |
end; |
| 1099 |
|
| 1100 |
if BackSpaceTabEnterString then |
| 1101 |
begin |
| 1102 |
case C of |
| 1103 |
CharBackSpace: begin Result := Result + 'BackSpace'; Exit; end; |
| 1104 |
CharTab : begin Result := Result + 'Tab' ; Exit; end; |
| 1105 |
CharEnter : begin Result := Result + 'Enter' ; Exit; end; |
| 1106 |
end; |
| 1107 |
end; |
| 1108 |
|
| 1109 |
case c of |
| 1110 |
CharEscape: Result := Result + 'Esc'; |
| 1111 |
CharDelete: Result := Result + 'Delete'; |
| 1112 |
' ' : Result := Result + 'Space'; |
| 1113 |
{ Show lowercase letters as uppercase, this is standard for showing menu item shortcuts. |
| 1114 |
Uppercase letters will be prefixed with Shift+. } |
| 1115 |
'a' .. 'z': Result := Result + Chr(Ord(C) - Ord('a') + Ord('A')); |
| 1116 |
CtrlA .. CtrlZ: Result := Result + Chr(Ord(C) - Ord(CtrlA) + Ord('A')); // we already added Ctrl+ prefix |
| 1117 |
else Result := Result + C; |
| 1118 |
end; |
| 1119 |
end; |
| 1120 |
|
| 1121 |
function MouseWheelDirection(const Scroll: Single; const Vertical: boolean): TMouseWheelDirection; |
| 1122 |
begin |
| 1123 |
if Scroll > 0 then |
| 1124 |
begin |
| 1125 |
if Vertical then Result := mwUp else Result := mwLeft; |
| 1126 |
end else |
| 1127 |
if Vertical then Result := mwDown else Result := mwRight; |
| 1128 |
end; |
| 1129 |
|
| 1130 |
{ TKeysPressed --------------------------------------------------------------- } |
| 1131 |
|
| 1132 |
function TKeysPressed.GetItems(const Key: TKey): Boolean; |
| 1133 |
begin |
| 1134 |
Result := Keys[Key]; |
| 1135 |
end; |
| 1136 |
|
| 1137 |
function TKeysPressed.GetStrings(const KeyString: String): Boolean; |
| 1138 |
begin |
| 1139 |
Result := (Length(KeyString) = 1) and Characters[KeyString[1]]; |
| 1140 |
end; |
| 1141 |
|
| 1142 |
function TKeysPressed.Modifiers: TModifierKeys; |
| 1143 |
begin |
| 1144 |
Result := ModifiersDown(Keys); |
| 1145 |
end; |
| 1146 |
|
| 1147 |
procedure TKeysPressed.KeyDown(const Key: TKey; const KeyString: String); |
| 1148 |
var |
| 1149 |
KeyChar: Char; |
| 1150 |
begin |
| 1151 |
if Key <> keyNone then |
| 1152 |
Keys[Key] := true; |
| 1153 |
|
| 1154 |
{ Although the API of TKeysPressed.KeyDown accepts String, |
| 1155 |
we can actually store only Char as being pressed for now, |
| 1156 |
in Characters and PressedCharacterToKey. } |
| 1157 |
if Length(KeyString) = 1 then |
| 1158 |
KeyChar := KeyString[1] |
| 1159 |
else |
| 1160 |
KeyChar := #0; |
| 1161 |
|
| 1162 |
if (Key <> keyNone) and |
| 1163 |
(KeyChar <> #0) and |
| 1164 |
(PressedKeyToCharacter[Key] = #0) then |
| 1165 |
begin |
| 1166 |
{ update Characters and PressedXxx mapping arrays } |
| 1167 |
if PressedCharacterToKey[KeyChar] = keyNone then |
| 1168 |
begin |
| 1169 |
Assert(not Characters[KeyChar]); |
| 1170 |
Characters[KeyChar] := true; |
| 1171 |
end else |
| 1172 |
begin |
| 1173 |
{ some key already recorded as generating this character } |
| 1174 |
Assert(Characters[KeyChar]); |
| 1175 |
Assert(PressedKeyToCharacter[PressedCharacterToKey[KeyChar]] = KeyChar); |
| 1176 |
|
| 1177 |
PressedKeyToCharacter[PressedCharacterToKey[KeyChar]] := #0; |
| 1178 |
PressedCharacterToKey[KeyChar] := keyNone; |
| 1179 |
end; |
| 1180 |
|
| 1181 |
PressedKeyToCharacter[Key] := KeyChar; |
| 1182 |
PressedCharacterToKey[KeyChar] := Key; |
| 1183 |
end; |
| 1184 |
end; |
| 1185 |
|
| 1186 |
procedure TKeysPressed.KeyUp(const Key: TKey; out KeyString: String); |
| 1187 |
var |
| 1188 |
KeyChar: Char; |
| 1189 |
begin |
| 1190 |
KeyChar := PressedKeyToCharacter[Key]; |
| 1191 |
if KeyChar <> #0 then |
| 1192 |
begin |
| 1193 |
{ update Characters and PressedXxx mapping arrays } |
| 1194 |
Assert(Characters[KeyChar]); |
| 1195 |
Characters[KeyChar] := false; |
| 1196 |
PressedCharacterToKey[KeyChar] := keyNone; |
| 1197 |
PressedKeyToCharacter[Key] := #0; |
| 1198 |
KeyString := KeyChar; |
| 1199 |
end else |
| 1200 |
KeyString := ''; |
| 1201 |
|
| 1202 |
Keys[key] := false; |
| 1203 |
end; |
| 1204 |
|
| 1205 |
procedure TKeysPressed.Clear; |
| 1206 |
begin |
| 1207 |
FillChar(Keys, SizeOf(Keys), 0); |
| 1208 |
FillChar(Characters, SizeOf(Characters), 0); |
| 1209 |
FillChar(PressedKeyToCharacter, SizeOf(PressedKeyToCharacter), 0); |
| 1210 |
FillChar(PressedCharacterToKey, SizeOf(PressedCharacterToKey), 0); |
| 1211 |
end; |
| 1212 |
|
| 1213 |
{ TInputPressRelease --------------------------------------------------------- } |
| 1214 |
|
| 1215 |
function TInputPressRelease.MouseWheel: TMouseWheelDirection; |
| 1216 |
begin |
| 1217 |
if EventType = itMouseWheel then |
| 1218 |
Result := MouseWheelDirection(MouseWheelScroll, MouseWheelVertical) else |
| 1219 |
Result := mwNone; |
| 1220 |
end; |
| 1221 |
|
| 1222 |
function TInputPressRelease.IsKey(const AKey: TKey): Boolean; |
| 1223 |
begin |
| 1224 |
Result := (AKey <> keyNone) and (EventType = itKey) and (Key = AKey); |
| 1225 |
end; |
| 1226 |
|
| 1227 |
function TInputPressRelease.IsKey(const AKey: TKey; const RequiredModifiers: TModifierKeys): Boolean; |
| 1228 |
begin |
| 1229 |
Result := IsKey(AKey) and (ModifiersDown = RequiredModifiers); |
| 1230 |
end; |
| 1231 |
|
| 1232 |
function TInputPressRelease.IsKey(AKeystring: String): Boolean; |
| 1233 |
begin |
| 1234 |
// only for backward compatibility (when this parameter was Char) convert #0 to '' |
| 1235 |
if AKeystring = #0 then |
| 1236 |
AKeystring := ''; |
| 1237 |
|
| 1238 |
Result := (AKeystring <> '') and (EventType = itKey) and (KeyString = AKeystring); |
| 1239 |
end; |
| 1240 |
|
| 1241 |
function TInputPressRelease.IsMouseButton(const AMouseButton: TCastleMouseButton): Boolean; |
| 1242 |
begin |
| 1243 |
Result := (EventType = itMouseButton) and (MouseButton = AMouseButton); |
| 1244 |
end; |
| 1245 |
|
| 1246 |
function TInputPressRelease.IsMouseWheel(const AMouseWheel: TMouseWheelDirection): Boolean; |
| 1247 |
begin |
| 1248 |
Result := (EventType = itMouseWheel) and (MouseWheel = AMouseWheel); |
| 1249 |
end; |
| 1250 |
|
| 1251 |
function TInputPressRelease.ToString: string; |
| 1252 |
begin |
| 1253 |
case EventType of |
| 1254 |
itKey: |
| 1255 |
Result := Format('key %s, character %s', |
| 1256 |
[ KeyToStr(Key), KeyStringToNiceStr(KeyString) ]); |
| 1257 |
itMouseButton: |
| 1258 |
Result := 'mouse ' + MouseButtonStr[MouseButton]; |
| 1259 |
itMouseWheel: |
| 1260 |
Result := Format('mouse wheel %s (amount %f, vertical: %s)', |
| 1261 |
[ MouseWheelDirectionStr[MouseWheel], |
| 1262 |
MouseWheelScroll, |
| 1263 |
BoolToStr(MouseWheelVertical, true) ]); |
| 1264 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1265 |
else raise EInternalError.Create('TInputPressRelease.Description: EventType?'); |
| 1266 |
{$endif} |
| 1267 |
end; |
| 1268 |
if KeyRepeated then |
| 1269 |
Result := Result + ', key repeated'; |
| 1270 |
if ModifiersDown <> [] then |
| 1271 |
Result := Result + ', modifiers pressed ' + ModifierKeysToNiceStr(ModifiersDown); |
| 1272 |
end; |
| 1273 |
|
| 1274 |
function TInputPressRelease.KeyCharacter: char; |
| 1275 |
begin |
| 1276 |
if Length(KeyString) = 1 then |
| 1277 |
Result := KeyString[1] |
| 1278 |
else |
| 1279 |
Result := #0; |
| 1280 |
end; |
| 1281 |
|
| 1282 |
function TInputPressRelease.Description: string; |
| 1283 |
begin |
| 1284 |
Result := ToString; |
| 1285 |
end; |
| 1286 |
|
| 1287 |
function InputKey(const Position: TVector2; const Key: TKey; |
| 1288 |
const KeyString: string; |
| 1289 |
const ModifiersDown: TModifierKeys): TInputPressRelease; |
| 1290 |
begin |
| 1291 |
FillChar(Result, SizeOf(Result), 0); |
| 1292 |
Result.Position := Position; |
| 1293 |
Result.EventType := itKey; |
| 1294 |
Result.Key := Key; |
| 1295 |
Result.ModifiersDown := ModifiersDown; |
| 1296 |
Result.KeyString := KeyString; |
| 1297 |
end; |
| 1298 |
|
| 1299 |
function InputMouseButton(const Position: TVector2; |
| 1300 |
const MouseButton: TCastleMouseButton; const FingerIndex: TFingerIndex; |
| 1301 |
const ModifiersDown: TModifierKeys): TInputPressRelease; |
| 1302 |
begin |
| 1303 |
FillChar(Result, SizeOf(Result), 0); |
| 1304 |
Result.Position := Position; |
| 1305 |
Result.EventType := itMouseButton; |
| 1306 |
Result.MouseButton := MouseButton; |
| 1307 |
Result.FingerIndex := FingerIndex; |
| 1308 |
Result.ModifiersDown := ModifiersDown; |
| 1309 |
end; |
| 1310 |
|
| 1311 |
function InputMouseWheel(const Position: TVector2; |
| 1312 |
const Scroll: Single; const Vertical: boolean; |
| 1313 |
const ModifiersDown: TModifierKeys): TInputPressRelease; |
| 1314 |
begin |
| 1315 |
FillChar(Result, SizeOf(Result), 0); |
| 1316 |
Result.Position := Position; |
| 1317 |
Result.EventType := itMouseWheel; |
| 1318 |
Result.MouseWheelScroll := Scroll; |
| 1319 |
Result.MouseWheelVertical := Vertical; |
| 1320 |
Result.ModifiersDown := ModifiersDown; |
| 1321 |
end; |
| 1322 |
|
| 1323 |
function InputMotion(const OldPosition, Position: TVector2; |
| 1324 |
const Pressed: TCastleMouseButtons; const FingerIndex: TFingerIndex): TInputMotion; |
| 1325 |
begin |
| 1326 |
FillChar(Result, SizeOf(Result), 0); |
| 1327 |
Result.OldPosition := OldPosition; |
| 1328 |
Result.Position := Position; |
| 1329 |
Result.Pressed := Pressed; |
| 1330 |
Result.FingerIndex := FingerIndex; |
| 1331 |
end; |
| 1332 |
|
| 1333 |
{ TCastleConfigKeysMouseHelper ----------------------------------------------- } |
| 1334 |
|
| 1335 |
function TCastleConfigKeysMouseHelper.GetKey(const APath: string; |
| 1336 |
const ADefaultValue: TKey): TKey; |
| 1337 |
begin |
| 1338 |
Result := StrToKey(GetValue(APath, KeyToStr(ADefaultValue)), ADefaultValue); |
| 1339 |
end; |
| 1340 |
|
| 1341 |
procedure TCastleConfigKeysMouseHelper.SetKey(const APath: string; |
| 1342 |
const AValue: TKey); |
| 1343 |
begin |
| 1344 |
SetValue(APath, KeyToStr(AValue)); |
| 1345 |
end; |
| 1346 |
|
| 1347 |
procedure TCastleConfigKeysMouseHelper.SetDeleteKey(const APath: string; |
| 1348 |
const AValue, ADefaultValue: TKey); |
| 1349 |
begin |
| 1350 |
SetDeleteValue(APath, KeyToStr(AValue), KeyToStr(ADefaultValue)); |
| 1351 |
end; |
| 1352 |
|
| 1353 |
end. |