Klasse PlayerKeyEvent


public final class PlayerKeyEvent extends PlayerEvent
Called when a player presses or releases a key on his keyboard.
Please note that you have to register all keys that need to be forwarded to the API. It's also necessary to enable key input first (see Player.setListenForKeyInput(boolean)
Example: Register keys and listen for key inputs
1@EventMethod
2public void onPlayerSpawn(PlayerSpawnEvent event){
3 Player player = event.getPlayer();
4
5 //Register keys 8, 9, 0, G and H
6 player.registerKeys(Key.Digit8, Key.Digit9, Key.Digit0, Key.G, Key.H);
7
8 //Important: Set listen for key input to true, otherwise the PlayerKeyEvent will not be triggered
9 player.setListenForKeyInput(true);
10}
11
12@EventMethod
13public void onKeyInput(PlayerKeyEvent event){
14 //We only want to process key presses (ignore key releases)
15 if(event.isPressed()){
16 //Check if A is pressed
17 if(event.getKey() == Key.A){
18 //Do something, e.g heal player
19 event.getPlayer().setHealth(100);
20 }
21 //Check if Enter is pressed
22 else if(event.getKey() == Key.Enter){
23 //e.g break player bones
24 event.getPlayer().setBrokenBones(true);
25 }
26 //Check if F1 is pressed
27 else if(event.getKey() == Key.F1){
28 //e.g heal broken bones
29 event.getPlayer().setBrokenBones(false);
30 }
31 }
32}
Siehe auch:
  • Methodendetails

    • getKey

      public Key getKey()
      Gets the key that is actuated.
      Gibt zurück:
      the key.
      Siehe auch:
    • isPressed

      public boolean isPressed()
      Gets whether the key is pressed or released.
      Gibt zurück:
      true if the key was pressed, false if it was released.