Crossfire JXClient, Trunk  R20561
KeyBindingState.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.keybindings;
23 
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
32 public class KeyBindingState {
33 
37  @Nullable
38  private final KeyBindings keyBindings;
39 
43  @Nullable
44  private final KeyBindings keyBindings2;
45 
49  @Nullable
50  private final CommandList commands;
51 
56  private int state;
57 
62  private int type = -1;
63 
68 
77  public KeyBindingState(@Nullable final KeyBindings keyBindings, @Nullable final KeyBindings keyBindings2, @Nullable final CommandList commands) {
78  this.keyBindings = keyBindings;
79  this.keyBindings2 = keyBindings2;
80  this.commands = commands;
81  }
82 
87  public void keyPressed(@NotNull final KeyEvent2 keyEvent) {
88  state = 1;
89  type = 0;
90  this.keyEvent = keyEvent;
91  }
92 
98  public boolean keyReleased() {
99  if (state == 0) {
100  return false;
101  }
102 
103  assert type != -1;
104  if (commands == null) {
105  if (keyBindings != null) {
106  keyBindings.deleteKeyBindingAsKeyCode(keyEvent);
107  }
108 
109  if (keyBindings2 != null) {
110  keyBindings2.deleteKeyBindingAsKeyCode(keyEvent);
111  }
112  } else {
113  if (keyBindings != null) {
114  keyBindings.addKeyBindingAsKeyCode(keyEvent, commands, false);
115  }
116  }
117 
118  return true;
119  }
120 
121 }
void addKeyBindingAsKeyCode(@NotNull final KeyEvent2 keyEvent, @NotNull final CommandList cmdList, final boolean isDefault)
Adds a key binding for a key code/modifiers pair.
void keyPressed(@NotNull final KeyEvent2 keyEvent)
Records a binding by key code.
Represents a pressed or released key.
Definition: KeyEvent2.java:33
int state
The dialog state: 0=waiting for initial key press, 1=waiting for key release.
KeyBindingState(@Nullable final KeyBindings keyBindings, @Nullable final KeyBindings keyBindings2, @Nullable final CommandList commands)
Creates a new instance.
final CommandList commands
The commands to bind, or.
int type
The type of key binding: -1=invalid, 0=key code (keyEvent is valid).
final KeyBindings keyBindings
The KeyBindings to modify.
Manages the state for the key binding dialog.
void deleteKeyBindingAsKeyCode(@NotNull final KeyEvent2 keyEvent)
Removes a key binding for a key code/modifiers pair.
final KeyBindings keyBindings2
The KeyBindings to modify.