News:

Congratulations!
 You've won the News Item Lottery! Your prize? Reading this news item! :)

get_tool_key does not detect TOOL_ZOOM_OUT

Started by Yona-TYT, July 12, 2026, 08:20:49 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Yona-TYT

Github commit: https://github.com/simutrans/simutrans/commit/38d065e195a6c9b49b44d8c3782b29ba3d7fb500
Topic: https://forum.simutrans.com/index.php/topic,23836.0.html

I'm implementing the keyboard shortcuts for multipak in the tutorial, however, I found a small issue.

The zoom_out id is not being detected, but for some reason the zoom_in id works fine.


In pak64 the keys are:
zoom in: >
zoom out: <

Currently the API only detects the ">" key id 0x2008


    // set text for shortcut key of any tool
    text.key_1 = get_tool_key(0x4004) // mailbox
    text.key_2 = get_tool_key(0x200c) // grid
    text.key_3 = get_tool_key(0x200d) // hide trees
    text.key_4 = get_tool_key(0x2008) // zoom in
    text.key_5 = get_tool_key(0x2009) // zoom out
    text.key_6 = get_tool_key(0x2010) // rotate map



prissi

Because there can be more than one definition of a tool but with different default strings. This function only returns the first definition.

Yona-TYT

I've verified through some debugging that the ASCII codes are being retrieved internally, but for some reason they aren't reaching the Script API.

See this:
Warning: My Debug: Current Tool = 0x2008 :: Ref = 0x9 :: key 62
Warning: My Debug: Current Tool = 0x2009 :: Ref = 0x9 :: key 60

Corresponding Characters:
  • 62: Character > (Greater-than sign)
  • 60: Character < (Less-than sign)

These characters are the correct ones for the Zoom in/Zoom out keys.



I suspect the values are being lost somewhere in this block:
/ just to save some typing ...

const uint16 key = tool->command_key;

/ no binding => finish

if (key == 0 || key == 0xFFFF) {

return "";

}

char mod[2];


mod[0] = tool->command_flags == 2 ? '+' : (tool->command_flags == 1 ? '^' : 0);
mod[1] = 0;

//keypad
if (key >= SIM_KEYCODE_NUMPAD_BASE && key < SIM_KEYCODE_NUMPAD_BASE + 10) {
char str[8];
sprintf(str, "%sNUM_%d", mod, key - SIM_KEYCODE_NUMPAD_BASE);
return str;
}

// function keys
if (key >= SIM_KEYCODE_F1 && key <= SIM_KEYCODE_F15) {
char str[8];
sprintf(str, "%sF%d", mod, key - SIM_KEYCODE_F1 + 1);
return str;
}

// special keys
switch(key) {
case ',': return "COMMA";
// case SIM_KEYCODE_SCROLLLOCK: return "SCROLLLOCK";
case SIM_KEYCODE_PAUSE: return "PAUSE";
case SIM_KEYCODE_HOME: return "HOME";
case SIM_KEYCODE_END: ��return "END";
case SIM_KEYCODE_SPACE: return "SPACE";
case SIM_KEYCODE_ESCAPE: return "ESC";
case SIM_KEYCODE_DELETE: return "DELETE";
case SIM_KEYCODE_BACKSPACE: return "BACKSPACE";
}

if (key < 32) {
char str[6];
sprintf(str, "^%c", key + 64);
return str;
}

if (key < 127) {
char str[2];
str[0] = key;
str[1] = 0;
return str;
}

char str[10];
sprintf(str, "%s#%d", mod, key);
return str;

Nazalassa

Returning local variable str? sounds suspicious to me... (Then maybe this is a specificity of C++ I am not aware of.)
Making paks since October 2023  |  pak48.bitlit | pak32.box | MLM for pak64 | Empire F7 cars | Pneumatic tubes | More pak64 vehicles and industries

Life is like a multi-tasking OS: you know you'll eventually get back to everything, but you don't know when.

prissi

It is a static string, so this is ok since it works with the other strings too.

I tested it with pak64 and it returned the correct key ...

Yona-TYT

Quote from: prissi on July 16, 2026, 08:19:04 AMIt is a static string, so this is ok since it works with the other strings too.

I tested it with pak64 and it returned the correct key ...

You're absolutely right, the character encoding is working perfectly as it should.

I've made a basic HTML mistake: the symbols "<" and ">" must be escaped.  :o

        if (tx_key == "<") {
            return "\"&lt;\""
        }
        else if (tx_key == ">") {
            return "\"&gt;\""
        }
        return "\""+tx_key+"\""


Captura desde 2026-07-16 08-54-20.png