Input in Forms
Introduction
Gum supports reading input from the mouse, touch screen, keyboard, and gamepads. Some types of input is automatically enabled such as clicks on Buttons. Other types of input must be enabled through code, such as giving controls focus for tabbing.
This tutorial covers the various ways input can be used to interact with forms objects.
Automatic Behavior
By default Gum controls work with the mouse and touch screen. The mouse automatically highlights all controls when it moves over the visible area of a control. Furthermore, the following events and actions also happen automatically:
Button
Click event
CheckBox
Click event
Checked event
Unchecked event
ComboBox
Expand/Collapse
Item selection
SelectionChanged event
ListBox
Item selection
SelectionChanged event
ItemClicked
ItemPushed
Scroll on mouse wheel
Menu
Item selection
Item expansion
Selected (MenuItem)
Clicked (MenuItem)
RadioButton
Click event
Checked event
Unchecked event
Slider
Drag thumb to change value
Click on track to change value
ValueChanged, ValueChangeCompleted, ValueChangedByUi events
TextBox/PasswordBox
Enter text with keyboard
CTRL+V paste
CTRL+C copy (TextBox only)
CTRL+X cut (TextBox only)
CTRL+A select all
Shift+arrow to select
Cursor drag to select
Caret movement with arrows
Delete and backspace to remove letters
Double-click to select all
Keyboard and Gamepad Input
Gum supports applying input from the keyboard and gamepads. By default the TextBox and PasswordBox forms types automatically receive input from the keyboard without any additional setup. All other interactive Forms controls can receive input from the keyboard and gamepads, but this input must be enabled.
To enable keyboard input, add the keyboard to the FrameworkElement.KeyboardsForUiControl list as shown in the following code block:
To enable gamepad input, add the gamepad to the FrameworkElement.GamePadsForUiControl list as shown in the following code block:
Notice that both KeyboardsForUiControl and GamePadsForUiControl are lists. If you do not want your game to support keyboard input you can keep the KeyboardsForUiControl empty. You can selectively add gamepads to GamePadsForUiControl depending on whether you only want some of the gamepads to control UI. For example, you may only want to read input from gamepads which have joined the game.
Once keyboard and gamepads are added to their appropriate lists, focused forms controls receive input from these input devices. For example, the following code shows how to set focus on a Button
which receives input from the keyboard.
The remainder of this tutorial excludes code to add keyboards and gamepads to their explicit list for brevity.
IsFocused and Tabbing
If your game includes multiple controls then the user can tab between the controls to pass focus. A forms control must first be given focus before tabbing is possible. Once a control is given focus, the user can tab to the next control using the tab key on the keyboard. Shift+tab tabs backwards (up) through the list of controls. Tabbing with the gamepad can be performed using up and down on the d-pad or analog stick.
The following code shows how to create multiple buttons. Once the first button is focused, the user can tab between the to click each one.
Tab order is controlled by the order that controls are added to their parent. If the parent is a StackPanel, then controls will naturally tab top to bottom. Once the last control receives focus, tabbing wraps back around to the first control. Similarly, pressing shift+tab on the first control wraps around to the last control.
Tab navigation stops on every control which implements the IInputReceiver
interface. Therefore, tabbing skips over forms controls such as Label, Image, Panel, and StackPanel. However, if the control contains children which implement IInputReceiver
, then those children can be focused through tabbing.
The following code shows that tabbing skips the labels and moves between each TextBox.
Customizing Tab Keys
The keyboard's tab and shift+tab keys are used to move focus between forms controls. This behavior can be customized by adding or removing KeyCombo
instances from FrameworkElement.TabKeyCombos
and FrameworkElement.TabReverseKeyCombos
.
For example, the following code adds the ability to tab by pressing the up and down arrows on the keyboard.
TabKeyCombos
and TabReverseKeyCombos
are lists and automatically include tab and shift+tab. You can clear these lists if you would like to prevent the tab key from moving focus.
Adding a KeyCombo to either of these lists enables navigation with these keys globally. In some cases this navigation may interfere with regular forms behavior. For example, adding Keys.Left
and Keys.Right
for tabbing prevents TextBox caret movement.
Tabbing on a case by case basis can be performed by subscribing to individual control events as shown in the following code. Only the Button
instances have left/right key tabbing while the Slider
does not tab with left/right so that it can use the arrow keys to change its value:
Last updated
Was this helpful?