| 1 |
{%MainUnit castlewindow.pas} |
| 2 |
{ |
| 3 |
Copyright 2004-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Small helpful utilities for CastleWindow implementations that are based |
| 18 |
on a system where left shift and right shift keys (or Alt, or Ctrl keys) |
| 19 |
are given as different keys. This situation requires some special care |
| 20 |
because in our TKey type (and so in TCastleWindow.KeysDown table) |
| 21 |
we do not differentiate between right and left Shift key (or Alt, or Ctrl). |
| 22 |
|
| 23 |
Currently this concerns GTK and Xlib implementations. |
| 24 |
|
| 25 |
If you want to use this file in some CastleWindow implementation, |
| 26 |
define CASTLE_WINDOW_USE_PRIVATE_MODIFIERS_DOWN when compiling that implementation. |
| 27 |
It is important to define CASTLE_WINDOW_USE_PRIVATE_MODIFIERS_DOWN |
| 28 |
instead of simply including this file in CastleWindow-implementation-specific |
| 29 |
file, because this allows ReleaseAllKeysAndMouse to use some |
| 30 |
special code when CASTLE_WINDOW_USE_PRIVATE_MODIFIERS_DOWN. |
| 31 |
|
| 32 |
Then you have to call SetPrivateModifiersDown with |
| 33 |
appropriate arguments at appropriate times. And that's all. |
| 34 |
SetPrivateModifiersDown will take care of calling DoKeyDown(k) and DoKeyUp(k) |
| 35 |
for you for k = keyShift, keyCtrl, keyAlt. } |
| 36 |
|
| 37 |
{$ifdef read_window_interface} |
| 38 |
private |
| 39 |
{ We keep here internally state of left shift key |
| 40 |
(in PrivateModifiersDown[mkShift, false]), right shift key |
| 41 |
(in PrivateModifiersDown[mkShift, true]) and the same for Alt and Ctrl key |
| 42 |
(so it's like PrivateModifiersDown[TModifierKey, IsRight]). |
| 43 |
I have to do this because Xlib differs left and right keys. |
| 44 |
And I want to set KeysDown[keyShift] always to "one of the shift keys is down". |
| 45 |
|
| 46 |
E.g. imagine this: |
| 47 |
1. user presses left shift key. We do DoKeyDown(keyShift). |
| 48 |
2. then he presses right shift key. Maybe we do DoKeyDown(keyShift), |
| 49 |
maybe not. It doesn't matter. What matters is that we have still |
| 50 |
KeysDown[keyShift] = true, no problem. |
| 51 |
3. now user releases left shift. And what ? We can't do DoKeyUp(keyShift) |
| 52 |
now, because keyShift should be still true (right shift is still down). |
| 53 |
So we have to keep somewhere states of left shift and right shift |
| 54 |
separately. (NOT only in KeysDown, where we have one keyShift) |
| 55 |
|
| 56 |
That's what PrivateModifiersDown is for. } |
| 57 |
PrivateModifiersDown:array[TModifierKey, boolean] of boolean; |
| 58 |
|
| 59 |
{ Set PrivateModifiersDown[Key, IsRight] := Value. |
| 60 |
Assume that k = ModifierKeyToKey[ModKey]. |
| 61 |
If Value then it can call DoKeyDown(k), if state of k should change. |
| 62 |
If not Value then it can call DoKeyUp(k), if state of k should change. } |
| 63 |
procedure SetPrivateModifiersDown(ModKey:TModifierKey; IsRight:boolean; |
| 64 |
Value:boolean); |
| 65 |
|
| 66 |
{$endif read_window_interface} |
| 67 |
|
| 68 |
{$ifdef read_implementation} |
| 69 |
|
| 70 |
procedure TCastleWindow.SetPrivateModifiersDown(ModKey:TModifierKey; IsRight:boolean; |
| 71 |
Value:boolean); |
| 72 |
var OldState, NewState:boolean; |
| 73 |
begin |
| 74 |
{ OldState and NewState are states of KeysDown[ModifierKeyToKey[ModKey]] } |
| 75 |
OldState:=PrivateModifiersDown[ModKey, true] or PrivateModifiersDown[ModKey, false]; |
| 76 |
PrivateModifiersDown[ModKey, IsRight]:=Value; |
| 77 |
NewState:=PrivateModifiersDown[ModKey, true] or PrivateModifiersDown[ModKey, false]; |
| 78 |
|
| 79 |
if OldState and (not NewState) then |
| 80 |
DoKeyUp(ModifierKeyToKey[ModKey]) else |
| 81 |
if (not OldState) and NewState then |
| 82 |
DoKeyDown(ModifierKeyToKey[ModKey], ''); |
| 83 |
end; |
| 84 |
|
| 85 |
{$endif read_implementation} |