programming input devices. getting the device state schemes for processing input – polling –...

16
Programming Input Devices

Upload: peregrine-logan

Post on 21-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Programming Input Devices

Page 2: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Getting the device state

• Schemes for processing input– Polling – Callbacks

• Ways to intercept messages from input devices– WM_INPUT– WM_MOUSEMOVE– DirectInput

Page 3: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Basic Mouse Interaction• Double Click

– WM_LBUTTONDBLCLK– WM_MBUTTONDBLCLK– WM_RBUTTONDBLCLK

• Button Press– WM_LBUTTONDOWN– WM_MBUTTONDOWN– WM_BBUTTONDOWN

• Button Release– WM_LBUTTONUP– WM_MBUTTONUP– WM_RBUTTONUP

• WM_MOUSEMOVE• WM_NCMOUSEMOVE

Page 4: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Keyboard Input

• Key inputs are handled through windows messages:– WM_KEYDOWN– WM_KEYUP– WM_CHAR

• The way the application uses the keyboard input will dictate which of these three inputs will be used– Using only WM_CHAR can enable Windows to factor in

other events or keystrokes such as Ctrl or Shift keys being pressed

Page 5: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Working with Keyboard• Character Code– ASCII or Unicode char. It is usually the value returned

by the C function getchar()• Virtual scan code– This is the value sent in the wParam variable for

WM_CHAR, WM_KEYDOWN and WM_KEYUP messages

• OEM scan code– This is the scan code provided by the OEM. It is useful

only if a code is required for a particular type of keyboard.

Page 6: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

The lParam Variable• This 32-bit variable, with 6 fields, is passed to the WndProc()– Bit 0-15 Repeat count; the number of times of

keystroke is repeated– Bit 16-23 8-bit OEM scan code– Bit 24 Extended key flag: 1 – extended key;

0 – otherwise– Bit 25-28 Reserved– Bit 29 Context code: 1 – if Alt key is pressed;

0 – otherwise– Bit 30 Previous key state: 0 -- previous key up,

1 – previous key down– Bit 31 Transition state: 0 – WM_KEYDOWN,

1 – WM_KEYUP

Page 7: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Keystroke Messages

• Keystroke messages come in the form of – WM_KEYDOWN– WM_KEYUP– WM_SYSKEYDOWN– WM_SYSKEYUP

• All keys produce WM_KEYDOWN and WM_KEYUP messages, except– Alt and F10

Page 8: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Keystroke Messages cont.

• OEM – identifies the key to the keyboard BIOS• Extended key flag – allows application to

recognize duplicate keys. For these, the value is 1– Ctrl key on right side of keyboard– Home, End, Insert, Delete, Page Up, Page Down– Number and arrow keys– Enter and forward slash (/)

• Transition state, previous key state, and context code are generally disregarded

Page 9: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Key Handling Notes

• WM_SYSKEYDOWN and WM_SYSKEYUP should not be processed in most cases by your code– These must eventually get to ::DefWindowProc, system

keyboard commands such as Alt-Tab and Alt-Esc will stop working

• Failure to pass these messages to the system can result in unusual and unreliable behavior

Page 10: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

Capturing Special keys

• Ctrl keys– wParam and extended bit

• Shift keys– wParam and Virtual key mapping

• Alt keys– Listen to WM_SYSCOMMAND

Page 11: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{int x=0,y=0;int repeat=0,extended=0,ext = 0,scanCode =0,

wasDown=0;char text[100];switch(message) {

Page 12: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

case WM_CHAR: { repeat = LOWORD(lParam);scanCode = HIWORD(lParam)&0x00FF;extended = (HIWORD(lParam) >>8 ) &1;wasDown = (HIWORD(lParam)>>30)&1;

sprintf(text,"WM_CHAR : Code == %d '%c' Repeat == %d SCANCODE == %d Extended = %d wasDown == %d ",wParam,wParam,repeat,extended,wasDown);

MessageBox(hWnd, text,"Keyboard Event Handling",MB_OK|MB_ICONINFORMATION);

}break;

Page 13: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

case WM_KEYDOWN:{repeat = LOWORD(lParam);scanCode = HIWORD(lParam)&0x00FF;extended = (HIWORD(lParam)>>8)&1;wasDown = (HIWORD(lParam)>>30)&1;

if(wParam == 17) // for Ctrl keyif(extended == 1)MessageBox (hWnd, "Right control key Pressed", "Title",

MB_OK|MB_ICONINFORMATION);elseMessageBox (hWnd, "Left control key Pressed", "Title",

MB_OK|MB_ICONINFORMATION);

Page 14: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

if(wParam == 16) { // Shift keyUINT virtualKey = MapVirtualKey(scanCode,3);if(virtualKey == VK_RSHIFT)

MessageBox( hWnd,"Right Shift key Pressed","Title", MB_OK|MB_ICONINFORMATION);

if(virtualKey == VK_LSHIFT)MessageBox(hWnd,"Left Shift key Pressed","Title",

MB_OK|MB_ICONINFORMATION);}else {

sprintf(text,"WM_KEYDOWN : Code == %d '%c' Repeat == %d scancode = %d Extended = %d ",wParam, wParam, repeat, extended);MessageBox(hWnd, text,"Keyboard Event Handling", MB_OK|MB_ICONINFORMATION);

} } break;

Page 15: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

case WM_SYSCOMMAND:MessageBox(hWnd,"Alt Key pressed", "Title", MB_OK|MB_ICONINFORMATION);break;

case WM_NCMOUSEMOVE:MessageBox(hWnd,"Moved on the Title bar","Mouse Event",

MB_OK|MB_ICONINFORMATION);break;

Page 16: Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –

case WM_LBUTTONDOWN:{x = LOWORD(lParam);y = HIWORD(lParam);sprintf(text,"Mouse left Click x == %d, y == %d",x,y);MessageBox(hWnd, text, "Mouse Event", MB_OK|MB_ICONINFORMATION);}break;