Class GInput¶
This class represents an input handler. An input handler receives mouse and keyboard
information, and makes it available to the user. To access mouse information, simply
access the attribute touch
. To access keyboard information, use one of the keyboard
methods like is_key_pressed
or is_key_down
.
You should never construct an object of this class. Creating a new instance of
this class will not properly hook it up to the keyboard and mouse. Instead, you should
only use the one provided in the input
attribute of GameApp. See
that class for more information.
Constructor¶
- class GInput¶
A class representing an input handler
An input handler receives mouse and keyboard information, and makes it available to the user. To access mouse information, simply access the attribute
touch
. To access keyboard information, use the methodis_key_down()
.You should never construct an object of this class. Creating a new instance of this class will not properly hook it up to the keyboard and mouse. Instead, you should only use the one provided in the input attribute of
GameApp
. See the documentation of that class for more information.Creates a new input handler
This constructor does very little. It does not hook up the handler to the mouse or keyboard. That functionality happens behind the scenes with hidden methods. You should only use use the object provided in the
input
attribute ofGameApp
. See the documentation of that class for more information.
Attributes¶
- touch_enabled¶
Whether the touch (mouse) interface is currently enabled.
Setting this value to False will disable all mouse clicks or drags. The value is True by default.
Invariant: Must be a bool
- keyboard_enabled¶
Whether the keyboard interface is currently enabled.
Setting this value to False will disable all key presses. The value is True by default.
Invariant: Must be a bool
Immutable Attributes¶
- touch¶
The current (x,y) coordinate of the mouse, if pressed.
This method only returns coordinates if the mouse button is pressed. If the mouse button is not pressed it returns None. The origin (0,0) corresponds to the bottom left corner of the application window.
There is currently no way to get the location of the mouse when the button is not pressed. This a limitation of Kivy.
Immutable: This value cannot be altered.
Invariant: Must be either a
Point2
or None (if there is no touch).
- key_count¶
The number of keys currently held down.
This attribute is a quick way to check whether the user has pressed any keys.
Immutable: This value cannot be altered.
Invariant: Must be an int > 0.
- keys¶
The list of keys that are currently held down.
Using this attribute is much slower than the method
is_key_down()
. You should use that method when you want to test a specific key. This attribute is primarily for debugging.Immutable: This value cannot be altered.
Invariant: Must be a list of strings (possibly empty)
Methods¶
- is_key_down(key)¶
Checks whether the key is currently held down.
A key is down so long as the user has it pressed down. A key can be down for multiple animation frames.
The key is a string describing the key pressed. For example, to determine whether the right-arrow key is down, use the method call:
input.is_key_down('right')
Similarly the method call:
input.is_key_down('w')
will indicate whether the W key is down. If key is the empty string ‘’, then this method will check if any key is down.
For a complete list of key names, see the Kivy documentation.
- Parameters:
key (
str
) – the key to test- Returns:
True if
key
is currently held down- Return type:
bool
- is_key_pressed(key)¶
Returns whether the key was just pressed.
A key is pressed if it is down this current animation frame, but was not down a previous animation frame.
The key is a string describing the key pressed. For example, to determine whether the right-arrow key is pressed, use the method call:
input.is_key_pressed('right')
Similarly the method call:
input.is_key_pressed('w')
will indicate whether the W key is pressed. If key is the empty string ‘’, then this method will check if any key is pressed.
For a complete list of key names, see the Kivy documentation.
- Parameters:
key (
str
) – the key to test- Returns:
True if
key
is currently held down- Return type:
bool
- is_key_released(key)¶
Returns whether the key was just released.
A key is released if it is up this current animation frame, but was down a previous animation frame.
The key is a string describing the key released. For example, to determine whether the right-arrow key is released, use the method call:
input.is_key_released('right')
Similarly the method call:
input.is_key_released('w')
will indicate whether the W key is released. If key is the empty string ‘’, then this method will check if any key is released.
For a complete list of key names, see the Kivy documentation.
- Parameters:
key (
str
) – the key to test- Returns:
True if
key
is currently held down- Return type:
bool
- is_touch_down()¶
Returns whether the mouse is currently held down.
If this method returns True, the attribute touch is guaranteed to not be None.
- Returns:
True if the mouse is currently held down; False otherwise
- Return type:
bool
- is_touch_pressed()¶
Returns whether the mouse is was just pressed.
The difference between a touch and a press is that a press means that the mouse is down this animation frame, but was not down the previous animation frame.
If this method returns True, the attribute touch is guaranteed to not be None.
- Returns:
True if the mouse is currently held down; False otherwise
- Return type:
bool
- is_touch_released()¶
Returns whether the mouse is was just released.
A release means that the mouse is not down this animation frame, but was down the previous animation frame.
If this method returns True, the attribute touch is guaranteed to be None.
- Returns:
True if the mouse is currently held down; False otherwise
- Return type:
bool