Page 1 of 1

Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 01:44
by Kenney
To add advanced functionality to mods adding custom keystrokes would be great. Bundling that together with pointing at nodes it could be used to give nodes more than 1 action (knocking on a door, opening/closing a door and locking/unlocking a door).

LUA example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_key({
   key = 'C',
   function = do_c()
})

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 04:10
by TriBlade9
I love the idea (and have wanted it for ages), but the problem is quite simply that everything runs on the server. A packet would have to be sent for every keypress, which is just nope.

*ahem*someone hurry up and write a system for client-side scripts*ahem*

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 04:36
by TG-MyinaWD
Yeah how about an Bind? like A mod allow players make binds, and it all can be saved in the player file or.. A %playername%_binds. But knocking on a door does sound interesting on Minetest. Specially for My Real Life subgame building one byte after another.

But anyway about the binding it maybe can be (well it will say in help) "/bind <key> <action/do>" and "/unbind <key>" to unbind it. that if all this is possible.

But I think we be cool see your Ideas Kenney come possible.
But be cool if we can add to an animation for knocking. and un/locking and open/close door. But how Sam Mine Swing his arm kinda looks like he is knocking.

But we do need more things to interact with the players and so on.

But all I know I think the knocking would be nice see.

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 05:52
by Zeno
TriBlade9 wrote:I love the idea (and have wanted it for ages), but the problem is quite simply that everything runs on the server. A packet would have to be sent for every keypress, which is just nope.

*ahem*someone hurry up and write a system for client-side scripts*ahem*


I'm not positive that a packet would have to be sent for *every* keypress. I.e. the client could be told upon connection what a key is bound to and just send a packet if that key is pressed.

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 14:47
by TriBlade9
Hmm, that would be possible. However, what about keyup/keydown handlers?

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 15:07
by thetoon
Zeno wrote:I'm not positive that a packet would have to be sent for *every* keypress. I.e. the client could be told upon connection what a key is bound to and just send a packet if that key is pressed.


It has to send on every keypress, as long as scripts are server-side only (IIRC, keypresses are sent no matter what, and server decides what to do with them).

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 16:05
by Calinou
Kenney wrote:LUA example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_key({
   key = 'C',
   function = do_c()
})


This has two problems:

  • How do you handle keyboard layouts other than QWERTY?
  • How do you let the client change that key, especially if they already use that key for something like movement?

I would rather have more “special” keys (special2, special3, special4 would be a good start), which the client can configure.

Re: Adding custom keystrokes through LUA

PostPosted: Thu Jan 15, 2015 16:08
by thetoon
Calinou wrote:This has two problems:

  • How do you handle keyboard layouts other than QWERTY?
  • How do you let the client change that key, especially if they already use that key for something like movement?

I would rather have more “special” keys (special2, special3, special4 would be a good start), which the client can configure.


This is already a problem, isn't it? And every other game (not to mention engines) already looked into it : picking ideas from competitors could be helpful here.

IMHO, client code should only send "actions" ("left", "right", "jump", "blahblah") and let the server decide what to do with it. The key/action mapping should be completely client side (it is already, I guess).

Re: Adding custom keystrokes through LUA

PostPosted: Sat Jan 17, 2015 08:10
by Kalabasa
Well then,
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_action("mymod:myaction", function(player)
  -- Example callback
  --   player = player who sent the action
  kill_player(player)
end)

-- This binding is sent to the client
minetest.register_keybind("mymod:myaction", {
  default_key = "X",
  description = "Suicide"
})

On connect, the server sends all custom keybindings to the client (all registered keybinds). The client then adds those to the "Change Keys" screen.

I don't know how keys currently work though, so this may be a trivial or a huge change.

Re: Adding custom keystrokes through LUA

PostPosted: Sat Jan 17, 2015 13:00
by Zeno
thetoon wrote:
Zeno wrote:I'm not positive that a packet would have to be sent for *every* keypress. I.e. the client could be told upon connection what a key is bound to and just send a packet if that key is pressed.


It has to send on every keypress, as long as scripts are server-side only (IIRC, keypresses are sent no matter what, and server decides what to do with them).


I wasn't talking about scripts (not really anyway). I was suggesting that if a server-side script created a custom key binding then that binding be sent to the client as part of the connection negotiation. This has several roadblocks, but it does not mean that every key press would need to be sent to the server.

(IIRC, keypresses are sent no matter what, and server decides what to do with them).


Currently key presses are not sent to the server at all (the key press in the client is converted into a directive if it's relevant). Why would the server need to know about key presses in the client?

Re: Adding custom keystrokes through LUA

PostPosted: Mon Jan 19, 2015 02:53
by spillz
I dabbled with making keyboard shortcuts for chat commands here:

https://github.com/minetest/minetest/pull/1217

If it ever got accepted I was going to come up with a system for letting mods send default shortcuts to clients.

Re: Adding custom keystrokes through LUA

PostPosted: Mon Jan 19, 2015 07:49
by rubenwardy
Please rebase it to the current master - without including all the upstream commits. I reckon it would be commited.