1 / 16

Programming Input Devices

Learn how to process input from various devices and handle key events in programming, including mouse interaction and keyboard input.

vsalinas
Download Presentation

Programming Input Devices

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Input Devices

  2. Getting the device state • Schemes for processing input • Polling • Callbacks • Ways to intercept messages from input devices • WM_INPUT • WM_MOUSEMOVE • DirectInput

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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 • AltandF10

  8. 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

  9. 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

  10. Capturing Special keys • Ctrl keys • wParam and extended bit • Shift keys • wParam and Virtual key mapping • Alt keys • Listen to WM_SYSCOMMAND

  11. 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) {

  12. 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;

  13. 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 key if(extended == 1) MessageBox (hWnd, "Right control key Pressed", "Title", MB_OK|MB_ICONINFORMATION); else MessageBox (hWnd, "Left control key Pressed", "Title", MB_OK|MB_ICONINFORMATION);

  14. if(wParam == 16) { // Shift key UINT 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;

  15. 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;

  16. 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;

More Related