View
239
Download
0
Category
Tags:
Preview:
Citation preview
COSC 4126 User Interaction
User Interaction
capturing and respondingto
input events
COSC 4126 User Interaction
Event handling in java
user action
input device
OS
JRE
AWT event queue
component event listeners
component+ event
COSC 4126 User Interaction
Events – message passing
AWTEvent ... ComponentEvent
... InputEvent
KeyEvent MouseEvent
... MouseWheelEvent
COSC 4126 User Interaction
Listening for events - interfaces
EventListener ... AWTEventListener all AWT events WindowListener window state KeyListener keyboard MouseListener click, press, enter MouseMotionListener move, drag MouseWheelListener
COSC 4126 User Interaction
constructing a listener
create component object to capture events (window, button, etc)
create object that implements listener interface code for reacting to event
attach listener to component
COSC 4126 User Interaction
Keyboard controlKeyListener methodspublic void keyTyped(KeyEvent e)
Invoked when a key has been typed. See the class description for KeyEvent for a definition of a key typed event.
(not often used in a real-time situation – includes multi-key combinations as single events)
public void keyPressed(KeyEvent e)
Invoked when a key has been pressed. See the class description for KeyEvent for a definition of a key pressed event.
public void keyReleased(KeyEvent e) Invoked when a key has been released. See the class
description for KeyEvent for a definition of a key released event.
COSC 4126 User Interaction
Keyboard control
KeyEventcontains information about the event
what character, key location (eg left/right shift)multiple formats, string, unicode, ...
for games, typically a small set of key actions are used for game control; other key events are ignored; note details in Brackeen (e.g., disabling tabs and alts)
example: KeyTest.java in chapter 3
COSC 4126 User Interaction
Mouse control MouseListenervoid mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on a component.
void mouseEntered(MouseEvent e) Invoked when the mouse enters a component.
void mouseExited(MouseEvent e) Invoked when the mouse exits a component.
for game interactionvoid mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component.
COSC 4126 User Interaction
Mouse control
MouseEvent which button is pressed, released mouse location
COSC 4126 User Interaction
Mouse control MouseMotionListenervoidmouseDragged(MouseEvent e)
Invoked when a mouse button is pressed on a component and then dragged. All dragged events go to component where button was pushed.
voidmouseMoved(MouseEvent e) Invoked when the mouse button has been moved on a component (with no buttons down).
COSC 4126 User Interaction
Mouse control
MouseWheelListenervoid mouseWheelMoved(MouseWheelEvent e)
Invoked when the mouse wheel is rotated.
MouseWheelEvent extends MouseEvent number of clicks (pos or neg) number of scroll units scroll type ( by line or by page )
example: MouseTest.java
COSC 4126 User Interaction
The user input “language” keyReleased(key) mousePressed(button),
mouseReleased(button)
keyPressed(key)
mouseMoved, mouseDragged mouseWheelMoved
change of state
single action
possible multiple or continuous action
COSC 4126 User Interaction
User controlattaching action to inputs
interface program - game
input action
update
point of view
point of view:
display on/off, tooltips, pan/zoom
COSC 4126 User Interaction
Mouselook – panning the view
(x,y)
draw fullscreen image four times at:
(x,y)
(x-w,y)
(x,y-h)
(x-w,y-h)
COSC 4126 User Interaction
Mouselook – Panning with mouse
mouseMoved event calculate change in mouse position apply change to image display point (x,y) reset mouse to screen centre
example: MouselookTest.java
COSC 4126 User Interaction
Robot classcontrolling mouse and keys
void keyPress(int keycode) Presses a given key.
void keyRelease(int keycode) Releases a given key.
void mouseMove(int x, int y) Moves mouse pointer to given screen coordinates.
void mousePress(int buttons) Presses one or more mouse buttons.
void mouseRelease(int buttons) Releases one or more mouse buttons.
void mouseWheel(int wheelAmt) Rotates the scroll wheel on wheel-equipped mice.
... other methods
COSC 4126 User Interaction
Brackeen’s Input Manager capture input Events manage Event interpretation manage connection of Events to GameActions control when GameActions are made
COSC 4126 User Interaction
InputManager class
keyActions
mouseActions
gameActions
update game state
listeners
1 - 1
remapped
COSC 4126 User Interaction
InputManagerTest example
game actions
moveLeft
moveRight
jump
pause
exit
keysspace
ADP
<esc>mouse
MOUSEBUTTON1MOUSEMOVELEFT
MOUSEMOVERIGHT
events
keyPressed
keyReleased
mousePressed
mouseReleased
mouseMoved
COSC 4126 User Interaction
Player class
moveLeft, moveRight, jump effect sprite Player extends Sprite class
state: jumping or normal floor: y-coordinate of normal pathvertical velocity set by jump
‘falls’ over time
0jump event
COSC 4126 User Interaction
Assignments:
first individual assignment – one week two interfaces
second group assignment – Thursday topic document – team plus topic
Recommended