Package lejos.hardware
Class Button
- java.lang.Object
-
- lejos.hardware.Button
-
public class Button extends java.lang.ObjectAbstraction for an NXT button. Example:Button.ENTER.waitForPressAndRelease(); Sound.playTone(1000, 1);
Notions: The API is designed around two notions: states (up / down) and events (press / release). It is said that a button is pressed (press event), if its state changes from up to down. Similarly, it is said that a button is released (release event), if its states changed from down to up. Thread Safety: All methods that return buttons states can be used safely from multiple threads, even while a call to one of the waitFor* methods active. However, it is not safe to invoke waitFor* methods in parallel from different threads. This includes the waitFor* methods of different buttons. For example Button.ENTER.waitForPress() must not be invoked in parallel to Button.ESCAPE.waitForPress() or the static Button.waitForAnyEvent(). In case this is needed, it is strongly recommended that you write your own Thread, which waits for button events and dispatches the events to anyone who's interested.
-
-
Field Summary
Fields Modifier and Type Field Description static KeyDOWNThe Down button.static KeyENTERThe Enter button.static KeyESCAPEThe Escape button.static java.lang.StringFREQ_SETTINGstatic intID_ALLstatic intID_DOWNstatic intID_ENTERstatic intID_ESCAPEstatic intID_LEFTstatic intID_RIGHTstatic intID_UPstatic Keyskeysstatic KeyLEFTThe Left button.static java.lang.StringLEN_SETTINGstatic KeyRIGHTThe Right button.static KeyUPThe Up button.static java.lang.StringVOL_SETTING
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voiddiscardEvents()This method discards any events.static intgetButtons()Low-level API that reads status of buttons.static intgetKeyClickLength()Return the current key click length.static intgetKeyClickTone(int key)Return the click freq for a particular key.static intgetKeyClickVolume()Return the current key click volume.static voidLEDPattern(int pattern)static intreadButtons()Low-level API that reads status of buttons.static voidsetKeyClickLength(int len)Set the len used for key clicksstatic voidsetKeyClickTone(int key, int freq)Set the frequency used for a particular key.static voidsetKeyClickVolume(int vol)Set the volume used for key clicksstatic intwaitForAnyEvent()Waits for some button to be pressed or released.static intwaitForAnyEvent(int timeout)Waits for some button to be pressed or released.static intwaitForAnyPress()Waits for some button to be pressed.static intwaitForAnyPress(int timeout)Waits for some button to be pressed.
-
-
-
Field Detail
-
ID_UP
public static final int ID_UP
- See Also:
- Constant Field Values
-
ID_ENTER
public static final int ID_ENTER
- See Also:
- Constant Field Values
-
ID_DOWN
public static final int ID_DOWN
- See Also:
- Constant Field Values
-
ID_RIGHT
public static final int ID_RIGHT
- See Also:
- Constant Field Values
-
ID_LEFT
public static final int ID_LEFT
- See Also:
- Constant Field Values
-
ID_ESCAPE
public static final int ID_ESCAPE
- See Also:
- Constant Field Values
-
ID_ALL
public static final int ID_ALL
- See Also:
- Constant Field Values
-
VOL_SETTING
public static final java.lang.String VOL_SETTING
- See Also:
- Constant Field Values
-
LEN_SETTING
public static final java.lang.String LEN_SETTING
- See Also:
- Constant Field Values
-
FREQ_SETTING
public static final java.lang.String FREQ_SETTING
- See Also:
- Constant Field Values
-
ENTER
public static final Key ENTER
The Enter button.
-
LEFT
public static final Key LEFT
The Left button.
-
RIGHT
public static final Key RIGHT
The Right button.
-
ESCAPE
public static final Key ESCAPE
The Escape button.
-
UP
public static final Key UP
The Up button.
-
DOWN
public static final Key DOWN
The Down button.
-
keys
public static final Keys keys
-
-
Method Detail
-
discardEvents
public static void discardEvents()
This method discards any events. In contrast toreadButtons(), this method doesn't beep if a button is pressed.
-
waitForAnyEvent
public static int waitForAnyEvent()
Waits for some button to be pressed or released. Which buttons have been released or pressed is returned as a bitmask. The lower eight bits (bits 0 to 7) indicate, which buttons have been pressed. Bits 8 to 15 indicate which buttons have been released.
-
waitForAnyEvent
public static int waitForAnyEvent(int timeout)
Waits for some button to be pressed or released. Which buttons have been released or pressed is returned as a bitmask. The lower eight bits (bits 0 to 7) indicate, which buttons have been pressed. Bits 8 to 15 indicate which buttons have been released.
-
waitForAnyPress
public static int waitForAnyPress(int timeout)
Waits for some button to be pressed. If a button is already pressed, it must be released and pressed again.- Parameters:
timeout- The maximum number of milliseconds to wait- Returns:
- the ID of the button that has been pressed or in rare cases a bitmask of button IDs, 0 if the given timeout is reached
-
waitForAnyPress
public static int waitForAnyPress()
Waits for some button to be pressed. If a button is already pressed, it must be released and pressed again.- Returns:
- the ID of the button that has been pressed or in rare cases a bitmask of button IDs
-
getButtons
public static int getButtons()
Low-level API that reads status of buttons.
-
readButtons
public static int readButtons()
Low-level API that reads status of buttons.
-
setKeyClickVolume
public static void setKeyClickVolume(int vol)
Set the volume used for key clicks- Parameters:
vol-
-
getKeyClickVolume
public static int getKeyClickVolume()
Return the current key click volume.- Returns:
- current click volume
-
setKeyClickLength
public static void setKeyClickLength(int len)
Set the len used for key clicks- Parameters:
len- the click duration
-
getKeyClickLength
public static int getKeyClickLength()
Return the current key click length.- Returns:
- key click duration
-
setKeyClickTone
public static void setKeyClickTone(int key, int freq)Set the frequency used for a particular key. Setting this to 0 disables the click. Note that key may also be a corded set of keys.- Parameters:
key- the NXT keyfreq- the frequency
-
getKeyClickTone
public static int getKeyClickTone(int key)
Return the click freq for a particular key.- Parameters:
key- The key to obtain the tone for- Returns:
- key click duration
-
LEDPattern
public static void LEDPattern(int pattern)
-
-