简体   繁体   English

当按下键盘按钮而不是循环播放时,如何执行一项操作

[英]How can I make one action occur when a keyboard button is pressed rather than looping

    if(Keyboard.isKeyDown(Keyboard.KEY_I)) {
        if(ms != MenubarState.INVENTORY) ms = MenubarState.INVENTORY;
        else ms = MenubarState.CLOSED;
    }

This bit here is ran every render, so it obviously will be checking while the button is pressed. 此位在每个渲染中都运行,因此显然在按下按钮时将进行检查。 I want to be able to press it once, and only execute it once until I take my finger off and press it again. 我希望能够按一次,并且只执行一次,直到我松开手指再按一次。

How is this done? 怎么做? I am programming in java. 我在用Java编程。

You could just remember if you executed the action: 您可能只记得是否执行了操作:

boolean done = false;
if(Keyboard.isKeyDown(Keyboard.KEY_I)) {
    if(!done){
       if(ms != MenubarState.INVENTORY) ms = MenubarState.INVENTORY;
       else ms = MenubarState.CLOSED;
       done= true;
    }
}
else done= false;

I would recomend you take a look at these: http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html https://en.wikipedia.org/wiki/Observer_pattern 我建议您看一下这些内容: http : //docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html https://en.wikipedia.org/wiki/Observer_pattern

You can create a class that implements the Keylisterner interface and extends the Observable class. 您可以创建一个实现Keylisterner接口并扩展Observable类的类。 You can have your class with your render loop in it implement the Observer interface. 您可以在您的类中使用您的渲染循环来实现Observer接口。 If you make your render class an observer to your keylistener class you can have the keylistener call a method containing: 如果使渲染类成为keylistener类的观察者,则可以让keylistener调用包含以下内容的方法:

if(ms != MenubarState.INVENTORY) ms = MenubarState.INVENTORY;
    else ms = MenubarState.CLOSED;

exactly once when the correct key is pressed. 当按下正确的键时,仅一次。

LWJGL comes with a Keyboard.getEventKeyState(); LWJGL带有Keyboard.getEventKeyState();。 If the key is being pressed, this will be true, false otherwise. 如果按下该键,则为true,否则为false。

if(Keyboard.isKeyDown(Keyboard.KEY_I)) {
    if(Keyboard.getEventKeyState())
        ms = MenubarState.INVENTORY;
    else ms = MenubarState.CLOSED;
}

What this does is it checks to see if the key is being held down. 它的作用是检查是否按住了该键。 If it is not, then the menu bar will be closed. 如果不是,则菜单栏将关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下键盘上的相应键时如何使按钮按下? JavaFX - How make a button pressed when corresponding key on a keyboard is pressed? JavaFX 如果按下按钮,如何使我可以跌落的单向平台 - How to make one way platform on which i can come down if button pressed 如何在按下按钮时使一段代码循环,并在不再按下按钮时将其停止? - How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed? 使用另一个按钮 - HYBRIS 进行操作时,如何禁用后台中的按钮? - How can I disable button in backoffice when make action with another button - HYBRIS? 按下按钮时如何更改图像视图的图标? - How can I change icon of image view when a button is pressed? 当按下后退按钮时,如何停止android中的asynctask? - How can I stop asynctask in android when back button is pressed? 按下键盘上的键时如何更改按钮的外观。 JavaFX - How to change an appearance of the button, when a key on a keyboard is pressed. JavaFx 如何让用户通过Stripe而非我的Wordpress网站进行付款? - How can I make users make payments via Stripe rather than my Wordpress site? Android Studio-按下按钮时循环播放动画 - Android Studio - Looping animation when button is pressed 如何制作一个JButton,使其在按下时执行新动作 - How to make a JButton that when pressed it does a new action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM