Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.11.2] Recommended way of checking KeyBindings for opening a GuiScreen


ctbe
 Share

Recommended Posts

I recently read somewhere in an answer to an old thread that rarely one should subscribe to KeyInputEvent when handling KeyBindings. That one should subscribe instead to ClientTickEvent. Of course, the answer was fitted to the context of that thread. In my case, in which all I do is open a GuiScreen menu should I subscribe with this

 

@Subscribe
def onKeyInput (keyInputEvent: KeyInputEvent): Unit = {
    // Do my logic here using KeyBinding.isKeyDown()
}



or with this

@Subscribe
def onClientTickEvent (clientTickEvent: ClientTickEvent): Unit = {
    // Do my logic here using KeyBinding.isKeyDown()
}



I have only one KeyBinding, the one for opening the gui.

For my purposes, I currently find no use for the keyInputEvent variable. Also, will subscribing to check for key inputs on the ClientTickEvent consume more processing power than using KeyInputEvent? I somehow feel it does because while I am not moving or doing anything, minecraft would still be checking for the key binding state every tick.

Link to comment
Share on other sites

5 minutes ago, ctbe said:

Also, will subscribing to check for key inputs on the ClientTickEvent consume more processing power than using KeyInputEvent? I somehow feel it does because while I am not moving or doing anything, minecraft would still be checking for the key binding state every tick.

Exactly the opposite, in fact. Minecraft already automatically updates the state of your KeyBinding instance whenever a keyboard event occurs. Checking for this updated state once a tick is perfectly fine and in fact it is what Minecraft does for all it's KeyBindings.

KeyInputEvent however potentially fires multiple times every tick, it fires whenever any keyboard input occurs, and it fires for every single event (key down, key up).

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.