[Solved] Get key_sequence of hotkeys

Place to get help with not working mods / modding interface.
Post Reply
apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

[Solved] Get key_sequence of hotkeys

Post by apriori »

...how?

Code: Select all

data:extend({
  {
    type = "custom-input",
    name = "modname-hotkey-main-window",
    key_sequence = "Y",
    consuming = "script-only"
  }
})
How to get string value of key_sequence using API?
Last edited by apriori on Tue Aug 30, 2016 2:40 pm, edited 2 times in total.
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Get key_sequence of hotkeys

Post by aubergine18 »

Just looked through relevant API docs and there doesn't seem to be any way whatsoever to get details about the key in-game.

You can get it during prototype initialisation by querying the custom-input objects in raw data table, so potentially you could choose different key if some other mod is already using your preferred key (would need to do it in data-final-fixes.lua as that's only place you could be sure all other mods have already defined their custom-inputs).

But there's no way to pass that information to something that control.lua could read...

...unless you go to the crazy extreme of defining new hidden item or tech that has it's .order set to value of key_sequence...

...but that's somewhat useless if user changes the keyboard shortcut via game options. :/

Observation: I found literally nothing that mentions custom-input in the official API guides (not even how to listen for the resulting event). Anyone reading the guide would be completely unaware that mods can define custom keyboard shortcuts.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: Get key_sequence of hotkeys

Post by apriori »

There's almost no information about hotkeys and styles/fonts/sprites in API guide. I don't know where to look for it - only by analyzing other's mods.

I want to get key_sequence string to show it in a tooltip of LuaGuiElement as a remembering for player. Some (me too) have too many mods and these mods register it's own hotkeys. As for me, I can't remember all of them, so "tooltip-note" seems a good idea to me...
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Get key_sequence of hotkeys

Post by aubergine18 »

Yup, I want to do similar, but can't just hard-code it in case the player changes the key via game options screen. Also, I want to be able to automatically choose alternate key if some other mod is already using the preferred key.

I tried searching forums but anything with an underscore gets converted in to separate words which screws the search results up.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13928
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Get key_sequence of hotkeys

Post by Rseding91 »

You can't read the key sequence through the game API because it can be different on each persons computer - it's not part of the game state.

It's the same reason you can't read translated text - each person can have a different language enabled.
If you want to get ahold of me I'm almost always on Discord.

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Get key_sequence of hotkeys

Post by Choumiko »

apriori wrote:

Code: Select all

data:extend({
  {
    type = "custom-input",
    name = "modname-hotkey-main-window",
    key_sequence = "Y",
    consuming = "script-only"
  }
})
Setting a gui tooltip to

Code: Select all

{"auto-trash-tooltip-pause"}
and in the locale file something like

Code: Select all

auto-trash-tooltip-pause = (__CONTROL__autotrash_pause__)
will show the hotkey for the custom input "autotrash_pause"

So probably best to ask the mod author(s) to add something like that.

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: Get key_sequence of hotkeys

Post by apriori »

Choumiko wrote:

Code: Select all

auto-trash-tooltip-pause = (__CONTROL__autotrash_pause__)
(__CONTROL__autotrash_pause__) - have you tried that? How can I find out which keywords should be used in this syntax construction? __CONTROL__what_should_be_here__
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Get key_sequence of hotkeys

Post by aubergine18 »

Great idea about having special locale scope, although to differentiate from normal scopes maybe have different token style? @keyfor/custom-input.my_name

When we get mod settings feature, it could potentially be extended to handle per-user mod settings too.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: Get key_sequence of hotkeys

Post by apriori »

Got it!

First of all you should "create" key sequence. You can do it in data.lua or in another lua (for example "prototypes/hotkey.lua") file and include it using require("prototypes.hotkey"):
data.lua
[Optional] Then you create an event handler for your hotkey. And make a function to show LuaGuiElement with required tooltip/text:
control.lua
Now you can include key sequence into locale:
locale.cfg
It works!
result
PS: I dunno what will be in this tooltip if I'll change key sequence using game menu... Maybe, old sequence will be shown.
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: [Solved] Get key_sequence of hotkeys

Post by aubergine18 »

Wow, the __CONTROL__ thing already existed?! Where do people get this information - I couldn't find this anywhere in API guide or wiki. @apriori do you have access to source code or something? Where did you find the infos?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: [Solved] Get key_sequence of hotkeys

Post by apriori »

aubergine18 wrote:Wow, the __CONTROL__ thing already existed?! Where do people get this information - I couldn't find this anywhere in API guide or wiki. @apriori do you have access to source code or something? Where did you find the infos?
No. To my sorrow. Read above, Choumiko gave me some rows, and I tried to use it...
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: [Solved] Get key_sequence of hotkeys

Post by DedlySpyder »

Factorio IRC is normally the best place to get undocumented information. I believe that's where we got the pastebin for custom inputs when they first came out.

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: [Solved] Get key_sequence of hotkeys

Post by apriori »

DedlySpyder wrote:Factorio IRC is normally the best place to get undocumented information. I believe that's where we got the pastebin for custom inputs when they first came out.
How to join?

EDIT: found)))
EDIT: not found((( Is that correct?
EDIT: alternative to desktop: http://webchat.esper.net/?channels=#Factorio
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: Get key_sequence of hotkeys

Post by apriori »

apriori wrote:Got it!

First of all you should "create" key sequence. You can do it in data.lua or in another lua (for example "prototypes/hotkey.lua") file and include it using require("prototypes.hotkey"):
data.lua
[Optional] Then you create an event handler for your hotkey. And make a function to show LuaGuiElement with required tooltip/text:
control.lua
Now you can include key sequence into locale:
locale.cfg
It works!
result
PS: I dunno what will be in this tooltip if I'll change key sequence using game menu... Maybe, old sequence will be shown.
Yes, old sequence is being shown.
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Get key_sequence of hotkeys

Post by DedlySpyder »

apriori wrote:...

Yes, old sequence is being shown.
Meaning that the tooltip doesn't update? How much have you tested it? I assume a restart wouldn't change it, but does redrawing the GUI update it?

apriori
Filter Inserter
Filter Inserter
Posts: 271
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Re: Get key_sequence of hotkeys

Post by apriori »

DedlySpyder wrote: Meaning that the tooltip doesn't update? How much have you tested it? I assume a restart wouldn't change it, but does redrawing the GUI update it?
No, updating (destroy -> add) doesn't help. I think, because tooltip is {"localized-string"} is being loaded on game launch.

EDIT: But if you change hotkey in game controls and restart a game - tooltip become affected by changes.
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

Post Reply

Return to “Modding help”