uInputManager: Add functions to get SDL key name
authorWuzzy <Wuzzy2@mail.ru>
Tue, 20 Feb 2018 22:21:33 +0100
changeset 13042 29357016f374
parent 13041 e0d5b63f4e37
child 13043 9119726e22bc
uInputManager: Add functions to get SDL key name
hedgewars/uInputHandler.pas
hedgewars/uTypes.pas
share/hedgewars/Data/Locale/de.txt
share/hedgewars/Data/Locale/en.txt
--- a/hedgewars/uInputHandler.pas	Tue Feb 20 19:15:47 2018 +0100
+++ b/hedgewars/uInputHandler.pas	Tue Feb 20 22:21:33 2018 +0100
@@ -27,6 +27,9 @@
 
 function  KeyNameToCode(name: shortstring): LongInt; inline;
 function  KeyNameToCode(name: shortstring; Modifier: shortstring): LongInt;
+
+function  KeyBindToCode(bind: shortstring): LongInt;
+function  KeyBindToName(bind: shortstring): ansistring;
 //procedure MaskModifier(var code: LongInt; modifier: LongWord);
 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
@@ -91,6 +94,47 @@
     MaskModifier(Modifier, code);
     KeyNameToCode:= code;
 end;
+
+// Takes a control name (e.g. 'quit') and returns the corresponding key code,
+// if it has been bound.
+// Returns -1 if the control has not been bound.
+function KeyBindToCode(bind: shortstring): LongInt;
+var code: LongInt;
+begin
+    code:= 0;
+    while (code <= cKeyMaxIndex) and (CurrentBinds[code] <> bind) do inc(code);
+    if code > cKeyMaxIndex then
+        // Return error
+        KeyBindToCode:= -1
+    else
+        KeyBindToCode:= code;
+end;
+
+// Takes a control name (e.g. 'quit') and returns the corresponding
+// human-readable key name from SDL.
+// FIXME: Does not work 100% for all keys yet, but at least it no
+//        longer hardcodes any key name.
+// TODO: Localize
+function KeyBindToName(bind: shortstring): ansistring;
+var code: LongInt;
+    name: ansistring;
+begin
+    code:= KeyBindToCode(bind);
+    if code = -1 then
+        KeyBindToName:= trmsg[sidUnknownKey]
+    else
+        begin
+        name:= SDL_GetKeyName(SDL_GetKeyFromScancode(code));
+        if (name <> '') then
+            KeyBindToName:= name
+        else
+            begin
+            WriteLnToConsole('Error: KeyBindToName('+bind+') failed to find SDL key name!');
+            KeyBindToName:= trmsg[sidUnknownKey];
+            end;
+        end;
+end;
+
 (*
 procedure MaskModifier(var code: LongInt; Modifier: LongWord);
 begin
--- a/hedgewars/uTypes.pas	Tue Feb 20 19:15:47 2018 +0100
+++ b/hedgewars/uTypes.pas	Tue Feb 20 22:21:33 2018 +0100
@@ -470,7 +470,7 @@
             sidNoEndTurn, sidNotYetAvailable, sidRoundSD, sidRoundsSD, sidReady,
             sidBounce1, sidBounce2, sidBounce3, sidBounce4, sidBounce5, sidBounce,
             sidMute, sidAFK, sidAutoCameraOff, sidAutoCameraOn, sidPressTarget,
-            sidNotAvailableInSD, sidHealthGain, sidEmptyCrate);
+            sidNotAvailableInSD, sidHealthGain, sidEmptyCrate, sidUnknownKey);
 
     // Events that are important for the course of the game or at least interesting for other reasons
     TEventId = (eidDied, eidDrowned, eidRoundStart, eidRoundWin, eidRoundDraw,
--- a/share/hedgewars/Data/Locale/de.txt	Tue Feb 20 19:15:47 2018 +0100
+++ b/share/hedgewars/Data/Locale/de.txt	Tue Feb 20 22:21:33 2018 +0100
@@ -93,6 +93,7 @@
 01:26=Werkzeug nicht verfügbar im Sudden Death
 01:27=+%1
 01:28=Leer!
+01:29=Unbekannte Taste
 
 ; Event messages
 ; Hog (%1) died
--- a/share/hedgewars/Data/Locale/en.txt	Tue Feb 20 19:15:47 2018 +0100
+++ b/share/hedgewars/Data/Locale/en.txt	Tue Feb 20 22:21:33 2018 +0100
@@ -93,6 +93,7 @@
 ; E.g. “+25” when gaining health from crate or vampirism
 01:27=+%1
 01:28=Empty!
+01:29=Unknown key
 
 ; Event messages
 ; Normal hog (%1) died (0 health)