Input Handling
Two SDL calls are used for input:
- SDL_PollEvent -- Polls for currently pending events.
- SDL_WaitEvent -- Waits indefinitely for the next available event.
When using these calls, the rule of thumb is you must call either SDL_PollEvent or SDL_WaitEvent less than .25 seconds (approximately) after your application's last call to one of these functions.
In this section:
Single Touch Input
The touchscreen interface is exposed using mouse events. For example, here is code for detecting a tap:
while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_MOUSEBUTTONDOWN: MyProcessTouchEvent(event.motion.x, event.motion.y);
Touch events are delivered using screen coordinates:
- X = 0 (left)-320 (right)
- Y = 0 (top)-480 (bottom) (Pixi's is 400)
Note that the screen resolution can vary depending on the device. To maintain forward compatibility, your application should use screen resolution to scale touch events.
For example:
SDL_VideoInfo *info = SDL_GetVideoInfo(); int width = info->current_w; ... case SDL_MOUSEMOTION: float scaledX = (float) event.motion.x / width; float scaledY = (float) event.motion.y / height;
Multiple Touch Input
Most device gestures are made with one finger, but some are made with more than one. Multiple touches are indicated with a mouse event field called event.motion.which. This field indicates the finger this event corresponds to (where active fingers are numbered starting at 0).
Note:
You can emulate two-finger, multiple-touch events in your desktop app by holding down the CTRL key and moving your mouse within the app's window. The second touch event is mirrored across the X/Y axis from the mouse location.
Two-finger input on the device

Gesture Area Input
You can make gestures (tap, swipe, drag, etc.) in two areas on your device:
- Touchscreen
- Gesture area
The gesture area is the black area extending from the bottom of the screen to halfway down the Center button (or where the Center button would be as displayed here).

* Gesture area
Note:
This section does not apply to desktop development, but you can emulate gesture area input using the keyboard.
For the most part, the gesture area is reserved for system functions. However, applications should use the back gesture in any area where there are hierarchical menus or a concept of back. These gestures are sent to the application in the form of SDL key events:
- PDLK_GESTURE_BACK
- PDLK_GESTURE_FORWARD
- PDLK_GESTURE_AREA
Note:
The PDLK_GESTURE_AREA key event is always sent whenever the gesture area is touched. This means PDLK_GESTURE_AREA is additionally sent with a back or forward gesture event.
Example of trapping a back or forward gesture:
while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == PDLK_GESTURE_BACK) printf("back\n"); else if (event.key.keysym.sym == PDLK_GESTURE_FORWARD) printf("forward\n"); break;
Accelerometer Input
The webOS device has an accelerometer—a small, internal mechanism that allows you to control the device by moving or tilting it. This capability is commonly used to change back and forth between portrait and landscape mode, or in other words, to reverse the aspect ratio and view the screen in terms of "up and down" or "side to side."
The webOS device contains a 3-axis accelerometer, which measures proper acceleration along each axis. The axes are labeled below:
Landscape Mode

3-axis accelerometer

Each axis measures direction and force. A zero result for a specific axis means there is no force acting on the accelerometer. For example, if you set the device flat on a table, the X and Y axes will measure at, or close to, zero. As you tilt the device left, the X axis acceleration becomes increasingly negative. If you then center it and tilt it right, it becomes increasingly positive.
The accelerometer is sampled at a fixed rate of 30 times per second. It is exposed to SDL applications using the Joystick abstraction.
To use the Joystick abstraction in your application:
-
At application launch, pass SDL_INIT_JOYSTICK to SDL_Init:
error = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ); -
After initializing, open the joystick device:
SDL_Joystick *joystick = SDL_JoystickOpen(0); -
In your game loop, you can poll the value with the SDL_JoystickGetAxis function, which returns a value between -32768 and 32768. The second parameter is the axis, 0 = x, 1 = y, 2 = z:
SDL_JoystickGetAxis(g_joystick, 0)For the value returned, -32768 = -1G and 32768 = 1G. You can convert this to a floating point value representing the number of whole Gs as follows:
float xAxisForceInGs = (float) SDL_JoystickGetAxis(g_joystick, 0) / 32768.0; float yAxisForceInGs = (float) SDL_JoystickGetAxis(g_joystick, 1) / 32768.0; float zAxisForceInGs = (float) SDL_JoystickGetAxis(g_joystick, 2) / 32768.0;
Accelerometer emulation on the desktop
To emulate accelerometer input on the desktop you have two choices:
- Use a joystick.
- Use the LEFT-SHIFT and mouse within your app's window on the desktop.
The following graphic illustrates how this would work. The points (.) indicate the mouse position:
Desktop accelerometer input emulation

These X,Y,Z values would correspond to the following device positions:
- (0,0,-1) -- The device is flat, as if lying face-up on a flat table with the user facing the bottom-edge.
- (1,0,0) -- The device is lying on its right-side edge.
- (-1,0,0) -- The device is lying on its left-side edge.
- (0,1,0) -- The device is lying on its bottom edge.
- (0,-1,0) -- The device is lying on its top edge.
In general, the closer the mouse moves to the center, the more X and Y return to 0, and Z returns to -1. If you are in the orange area, your XYZ is the same as that of a point on the circle edge that intersected a straight line drawn from the orange location to the circle center.
Keyboard Input
Keyboard input is delivered using the SDL_KEYDOWN and SDL_KEYUP events. The ASCII character code is stored in event.key.keysym.sym.
Example of code handling keyboard input:
while ( SDL_PollEvent (&event) ) { switch (event.type) { case SDL_KEYDOWN: int ascii_key_pressed = event.key.keysym.sym;