# HG changeset patch # User Wuzzy # Date 1519161693 -3600 # Node ID 29357016f374d7f6d4aae7ee808b7e30b6c20308 # Parent e0d5b63f4e376e52364c786534cbeb7823314c5e uInputManager: Add functions to get SDL key name diff -r e0d5b63f4e37 -r 29357016f374 hedgewars/uInputHandler.pas --- 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 diff -r e0d5b63f4e37 -r 29357016f374 hedgewars/uTypes.pas --- 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, diff -r e0d5b63f4e37 -r 29357016f374 share/hedgewars/Data/Locale/de.txt --- 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 diff -r e0d5b63f4e37 -r 29357016f374 share/hedgewars/Data/Locale/en.txt --- 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)