Started by
ahznb
on
Topic category: Help with Minecraft modding (Java Edition)
Hi,
I've created a new key binding procedure. The generated procedure only activates on key press and does not have the pressed "key" as dependency, and there are no preset procedures to query key state. I'd like the procedure to query the key press state, like if it has been released, how long the key has been pressed, etc. How do I do that with minimal coding, or better yet without coding?
Thanks!
Edited by ahznb on Wed, 11/27/2019 - 04:35
Detecting key unpress is not possible yet without coding. I suggest you open a support ticket requesting this feature and we will consider adding it.
You can count the time between two presses by making a global timer and use the timer variable to count the difference between two checks. Global timer: https://www.youtube.com/watch?v=FtyyMz6rBXc&list=PLAeL-oIFIEngE6jRgFYeFMfuj8WQsO3Ei&index=56&t=1s
Thanks. I will try to open a ticket.
Can you give me a simple example how to detect key press time with minimal coding?
I do not have any example for this right now as I don't know how to detect key release yet. I have an idea on how one would do this, but it would require Java knowledge to actually implement this for your case.
If you don't know Java, I suggest you open a ticket and wait for us to implement this.
Here is a topic I found for 1.7.10, but checking for key binding did not change that much: https://www.minecraftforge.net/forum/topic/33522-1710-keybinding-keyup-method/
Basically you would need to store previous value and current click value on input event and check if the state went from pressed to released and trigger procedure in such case.
Thanks.
But I have another question about the code. Why is keybinding event doing this:
instead of just calling the handler directly in onKeyInput? Can I just call the handler directly? If not, how do I pass on more arguments to onMessage?
Because keypress happens on the client side, but events need to happen on the server-side to propagate to all clients, otherwise you will have pretty strange things happening.
To pass more data, just adds new fields to KeyBindingPressedMessage and properly store them to NBT data that is passed to the server side.
So I basically added a new int "dummy" in KeyBindingPressedMessage, and set it in onKeyInput, like this:
But when I access it here:
its value is always 0. What did I do wrong?
Use toBytes and fromBytes methods to store the data to ByteBuf which is then sent to the server.
Oops, wrong code.
This is the "real" code:
Oh, that's beyond me then. Do I need to implement toBytes and fromBytes to read/write from buf to dummy? When I create a new KeyBindingPressedMessage do I need to allocate memory for a buffer or can I use dummy as a 4 byte buffer?
Buffer will do all the allocation. You can check GUI mod element code for button events, as for buttons, button ID is passed in the message.
Wow, I just love sample code! Thanks! That did it.
You are welcome! :D
So how do you make it?