Title: Mouse and Keyboard
1Mouse and Keyboard
2THE MOUSE
- A pointing device with one or more buttons.
- When the user moves the physical mouse, Windows
moves a small bitmapped image (the mouse cursor)
on the display - One pixel in the cursor is the "hot spot" it
points to a precise location on the display - The hot spot position is constantly updated by
low-level logic inside Windows - Mouse actions by the user
- Moving
- Clicking (press/release)
- Double Clicking
- Dragging (moving while holding down a button).
3Mouse messages
- Client area messages
- mouse message generated whenever the mouse passes
over the window or is clicked within the window - 21 messages in all
- E.g. WM_MOUSEMOVE , WM_LBUTTONUP
- Non-client area messages
4WM_MOUSEMOVE Message
- sent to the program whose window is under the
cursor each time the mouse moves - HIWORD(lParam) Y position in the client area
using "client area coordinates" (pixels)
relative to the upper lefthand corner of the
client area. - LOWORD(lParam) X position.
- wParam indicates if a mouse button, Shift key, or
control key was pressed as the message was
sent--Mouse Notification codes (MK_LBUTTON,
MK_RBUTTON, MK_SHIFT, MK_CONTROL,
MK_MBUTTON).
5WM_BUTTON
- L, M, R
- DOWN, UP, DBLCLK
- (The DBLCLK message is sent only if the
wndclass.style contains CS_DBLCLKS when the
class is registered)
6INPUT FOCUS
- If more than one instance of a program is
running, only one will have a highlighted top
caption line - That program's window is said to have the "input
focus - This is the only window that will receive any
keyboard input - not significant for mouse input because we
normally use the mouse to pick an active window
7Gaining/Losing the input focus
- When a window gains (loses) the input focus,
Windows sends a WM_SETFOCUS (WM_KILLFOCUS)
message to the window - Common responses are to highlight an edit area or
change a caption. - to explicitly give a window or window control the
input focus we can use - SetFocus(hwnd)
8Reaction to receiving focus
- caption-barred windows have their caption bars
highlighted - edit controls start showing a blinking caret at
the point of keyboard input - button controls show a highlighted outline
- list boxes show a highlighted selection item
- combo boxes display their list box or highlight
it (if it's displayed).
9NONCLIENT MOUSE MESSAGES
- Outside client area WM_NC messages are sent
(MOUSEMOVE, etc.) - lParam contains the mouse position, but in screen
coordinates - wParam indicates the non-client area where the
mouse action occurred - Usually our programs don't process nonclient
mouse messages
10Capturing the mouse
- limiting the mouse to interact with just one
program - An application that does this has "captured" the
mouse - Only this application will receive mouse messages
- To do this SetCapture(hWnd)
- To release it ReleaseCapture(void)
11The keyboard
- The keyboard provides the primary means of
entering text and numbers - Keypresses can also be used as shortcuts for
mouse actions (accelerators) - Character sets
- 7-bit ASCII, for letters, numbers and some common
keyboard symbols - IBM-PC, additional graphic/special symbols
- ANSI, Less graphics, more accented/international
symbols - UNICODE, a uniform 16-bit code which allows the
representation of every character in every
written language of the world
12ASCII code
13ANSI code
14Compatibility
- the standard C compiler library functions
(ischar(), toupper(), tolower()) assume ASCII
characters and ignore accented characters - if we use these, our programs will not work right
in many foreign languages - Windows provides its own functions for
using/converting characters - To deal correctly with accented characters
- They are part of Windows, not part of our program
15Windows functions
AnsiLower() Convert char string to
lowercase AnsiLowerBuff() Same, doesn't
have to be null terminated AnsiNext()
Move to next char in a string AnsiPrev()
Move to previous char in a string AnsiToOem()
Convert a char string from ANSI to OEM
AnsiToOemBuff() Same, doesn't have to be null
terminated AnsiUpper() Convert char
string to uppercase AnsiUpperBuff()
Same,... IsCharAlpha() Determine if an
ANSI char is an alphabetic char
IsCharAlphaNumeric() Determine if an ANSI char
is alphabetic/numeric IsCharLower()
Determine if an ANSI char is lowercase
IsCharUpper() Determine if an ANSI char
is uppercase OemToAnsi() Convert a
char string from OEM to ANSI OemToAnsiBuff() Same
,... ToAscii() Convert from virtual
key/scan code data to ANSI
16UNICODE
- 7/8 bit character codes are unsatisfactory for
many foreign languages - Unicode is a character coding that uses a uniform
16-bit code for each character - No support in Windows 3.xx, rudimentary support
in Windows 95 , complete support in Windows NT - In C/C, wide character type, wchar_t is used
for unicode
17Keyboard message processing
- The Keyboard driver's SETUP copies the selected
country's keyboard driver into the
\windows\system\KEYBOARD.DRV file - Windows starts gt KEYBOARD.DRV enabled, and this
responds by saving and updating the keyboard INT
9 vector - From then on KEYBOARD.DRV intercepts INT 9 (key
press/release) from the keyboard
18Keyboard message processing (contd)
- KEYBOARD.DRV decodes the key and calls a Windows
routine that stores it as a queued message in
system queue - Windows transfers the message to the correct
application's queue - Our program gets the message from the
GetMessage() loop. This is polling--like
DOS INT 21h, Service 01 or BIOS INT 16h).
It's not asynchronous gt our program is never
interrupted. This gives all the benefits,
but none of the headaches of INT 9!!
19Keyboard message processing (contd)
- User presses a key gt Windows sends a WM_KEYDOWN
message to the window with the input focus - Key released gt WM_KEYUP message
- wParam contains device-independent "virtual key
code" for the key pressed/released - Most keys have VK codes ANSI code
- Others have names like VK_F1, VK_TAB
- A program can use VK codes to figure out what the
user is doing with keys
20(No Transcript)
21Key down/up messages
- put data into the lParam
- bit-31 1gtkey being pressed 0gtkey being
released. - bit-30 1gtkey was down before message was sent
0gtnot down - bit-29 1gtltALTgt was held down when key was
pressed 0gtnot down - 25-28 Reserved
- bit-24 1gtextended key flag (e.g., function
key or numeric keypad key) - 16-23 Keyboard OEM scan code (the value in AH
after INT 16h returns) - 0-15 Repeat count ( of times char was
repeated because held down key).
22TranslateMessage()
- converts the current combination of letter and
shift keys to an equivalent ANSI code - TranslateMessage() detects a keyboard action that
translates to an ANSI character, it generates a
WM_CHAR message - The wParam will have the ANSI code
- Function keys and keys that don't have ANSI codes
don't generate WM_CHAR messages
23TranslateMessage() (contd)
- assuming the event loop has a TranslateMessage()),
the sequence of messages sent is - WM_KEYDOWN, key pressed
- WM_CHAR, ANSI char provided if there is one
- WM_KEYUP key released
24System key messages
- when no window has the input focus , or for the
ltALTgt key the message sequence is different - WM_SYSKEYDOWN -- a key was depressed
- WM_SYSCHAR -- ANSI character (if there was
one). - WM_SYSKEYUP -- Key released.
- ltALTgt keystroke indicates some special function.
Usually we use WM_SYSKEYDOWN to determine which
VK key was depressed - lParam and wParam are used the same as before
25FONT
- There are three basic kinds of fonts
- Stock fonts--built into Windows, always available
- Logical or GDI fonts
- Stroke fonts are continuously scalable , .fon
- Raster fonts are bitmaps, only integer scaling ,
.fon - TrueType fonts are really rasterized stroke
fonts .fot/.ttf - Device fonts--native to the output device (e.g.,
built-in printer fonts).
26Stock fonts
27Stroke fonts
28Raster fonts
29TrueType fonts
30Using stock fonts
- HDC hDC
- HFONT hFont
- hDC GetDC(hWnd)
- hFont GetStockObject (ANSI_VAR_FONT)
- SelectObject (hDC,hFont) / now can use it /
31Using Logical Fonts
- Called logical since they come from program logic
not just from a file - Use CreateFont() instead of GetStockObject() to
load and get a font handle - Font is loaded from a separate file .fon,
.fot/.ttf - hFont CreateFont(Ht, Width, Escapement,
Orientation , Weight , Italic , Underline,
StrikeOut , CharSet , OutputPrecision ,
ClipPrecision , Quality , PitchAndFamily ,
Facename)
32Escapement and orinetation
33Text metrics
- we may not know all of the dimensions of a font
after it's created - use GetTextMetrics() to find out font details
34Keyboard accelerators
- Provide an alternative to using the mouse to
select menu items - The simplest way of doing it is to precede the
menu item's title string with an '' in the
resource script file - MyMenu MENU
- BEGIN
- MENUITEM "Paint", IDM_PAINT
- MENUITEM "Clear", IDM_CLEAR
- ...
- END
35Keyboard accelerators(contd)
- But if there are popup menus , using ALT key is
cumbersome - A better shortcut would require only one key
combination to execute the command - We can do this by processing WM_KEYDOWN and
WM_CHAR messages - But Windows gives a much simpler alternative
- define the key combinations in an "accelerator
table" in the resource file - Each defined key combination sends a WM_COMMAND
message to the program.
36Keyboard acceleratorsExample
- MyAccel ACCELERATORS
- BEGIN
- VK_F1 , IDM_ANSI , VIRTKEY
/ F1 activates / - VK_F2, IDM_OEM, VIRTKEY, CONTROL /
ltCtrlgtltF2gt activates / - "A", IDM_NOMENU, VIRTKEY, ALT /
ltAltgtltAgt activates / - "Z", 36, SHIFT, CONTROL /
ltShiftgtltCtrlgtltZgt activates/ -
/ LOWORD(wParam) 36 /
37Using accelerator tables
- Use LoadAccelerators() to load the table
- TranslateAccelerator()--translates
WM_KEYUP/WM_KEYDOWN messages into WM_COMMAND
messages (if there is an entry in the
application's accelerator table). This must be
placed in the WinMain()'s message loop