Download presentation
Presentation is loading. Please wait.
Published bySamantha Amice Lang Modified over 8 years ago
1
Chapter 3 Keyboard Input Department of Digital Contents Sang Il Park
2
Midterm Announcement Midterm – Coding Test: –On Apr. 26th (2012/4/26) Thursday –At 10:30 (Class 1) / 12:00 (Class 2) –In the Classroom ( 율곡관 304) –5 problems to code in time. Problem Sets: on our Homepage –3 problems will be chosen from the problem set: those problems are exactly same. –1 problem will be similar to one in the problem set. –1 problem will be new.
3
Questions are always welcome!! Try to solve the problems by yourself, and
4
4 The Input Focus Windows decide which window to send the mouse and keyboard message –Mouse messages go to the window under the cursor –Keyboard messages go to the window with the ‘input focus’ Input focus: –Active application has the input focus –Only one window has the input focus at any given time
5
5 The Input focus Evidence of having the ‘input fucus’ carot
6
6 The Input Focus Receive or lose the input focus WM_SETFOCUSWM_KILLFOCUS WM_SETFOCUS
7
The Input Focus functions request the input focus Find out the window currently having the input focus SetFocus (); CWnd * pFocusWnd = GetFocus ();
8
8 Keystroke Messages Reporting key presses and releases MessagesMeaning WM_KEYDOWNWhen pressing keys (except F10 and Alt) WM_KEYUPWhen releasing keys (except F10 and Alt) WM_SYSKEYD OWN When pressing F10, Alt and Alt+Key WM_SYSKEYU P When releasing F10, Alt and Alt+Key
9
9 Message Handler Prototype of the message handlers –nChar Virtual key code of the key –nRepCnt Repeat count: Number of repeated keystrokes –nFlags Information of the key (read the textbook) afx_msg void On#### (UINT nChar, UINT nRepCnt, UINT nFlags) ;
10
10 Virtual Key Code Virtual Keycode VK_CANCEL VK_BACK VK_TAB VK_RETURN VK_SHIFT VK_CONTROL VK_MENU VK_PAUSE VK_CAPITAL VK_ESCAPE VK_SPACE VK_PRIOR VK_NEXT VK_END Key Ctrl-Break Backspace Tab Enter Shift Ctrl Alt Pause Caps Lock Esc Spacebar PgUp PgDn End Virtual Keycode VK_HOME VK_LEFT VK_UP VK_RIGHT VK_DOWN VK_SNAPSHOT VK_INSERT VK_DELETE VK_0 - VK_9 VK_A - VK_Z VK_F1 - VK_F12 VK_NUMLOCK VK_SCROLL Key Home ← ↑ → ↓ Print Screen Insert Delete 0 – 9 A – Z F1 - F12 Num Lock Scroll Lock VK_SCROLLScroll Lock
11
Shift states Shift, Control and Alt keys are used for combination with other key Information about those keys shift states reporting a key is held down or not: –Returns a negative value when key is held down or a nonnegative if it is not. GetKeyState ( Virtual Keycode );
12
Coding practice Moving Rect VK_LEF T VK_UP VK_RIG HT VK_DO WN 1. Create a variable for position CPoint m_pt; 2. Draw a rectangle at the position (OnPaint) dc.Rectangle(m_pt.x, …); 3. Add OnKeyDown Handler 4. Change position w.r.t. nChar if(nChar == VK_RIGHT) m_pt.x += 10; 1. Create a variable for position CPoint m_pt; 2. Draw a rectangle at the position (OnPaint) dc.Rectangle(m_pt.x, …); 3. Add OnKeyDown Handler 4. Change position w.r.t. nChar if(nChar == VK_RIGHT) m_pt.x += 10; void OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags) ;
13
13 Character Messages Possible scenarios when pressing VK_R key: charPossible Virtual Key Combination r In English input mode, press VK_R or VK_R + Caps Lock + Shift R In English input mode, press VK_R + Caps Lock or VK_R + Shift ㄱ In Korean input mode, press VK_R ㄲ In Korean input mode, press VK_R + Shift
14
14 Character Message When pressing VK_R key When pressing Alt + VK_R Key WM_KEYDOWN WM_CHAR WM_KEYUPWM_SYSKEYDOWN WM_SYSCHAR WM_SYSKEYDOWN WM_SYSKEYUP WM_KEYUP Alt R R
15
15 Message Handler Prototype of the handler –nChar Character Code (ANSI / unicode) –nRepCnt Repeat count –nFlags More Information afx_msg void OnChar (UINT nChar, UINT nRepCnt, UINT nFlags) ; afx_msg void OnSysChar (UINT nChar, UINT nRepCnt, UINT nFlags) ;
16
Coding Practice Typing application void CMainWindow::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags) { if (((nChar >= _T (`A')) && (nChar <= _T (`Z'))) || ((nChar >= _T (`a')) && (nChar <= _T (`z')))) { // Display the character } else if (nChar == VK_RETURN) { // Process the Enter key } else if (nChar == VK_BACK) { // Process the Backspace key } void CMainWindow::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags) { if (((nChar >= _T (`A')) && (nChar <= _T (`Z'))) || ((nChar >= _T (`a')) && (nChar <= _T (`z')))) { // Display the character } else if (nChar == VK_RETURN) { // Process the Enter key } else if (nChar == VK_BACK) { // Process the Backspace key }
17
17 Caret Related Functions FunctionDescription CreateCaretCreates a caret from a bitmap CreateSolidCaretCreates a solid line caret or a block caret CreateGrayCaretCreates a gray line caret or a block caret GetCaretPosRetrieves the current caret position SetCaretPosSets the caret position ShowCaretDisplays the caret HideCaretHides the caret
18
18 Caret void CMainWindow::OnSetFocus(CWnd* pOldWnd) { CWnd ::OnSetFocus(pOldWnd); CreateSolidCaret(20, 20); SetCaretPos(CPoint(50, 50)); ShowCaret(); } void CMainWindow::OnKillFocus(CWnd* pNewWnd) { CWnd ::OnKillFocus(pNewWnd); HideCaret(); ::DestroyCaret(); }
19
Virtual Keyboard Application Read the Example in the text book
20
Chapter 14 Timer Department of Digital Contents Sang Il Park
21
TIMER When you need to set a repeating job
22
SetTimer function Sending WM_TIMER message at specified interval –id id of the timer(ex: 0, 1, 2, … ) For indentifying multiple timers –time: interval (=millisec) 1000 = 1 second Call this function after creating the window –Usually set the timer in OnCreate function (the handler for WM_CREATE message) void SetTimer (int id, int time, void * fp) ; Ex) SetTimer(0, 100, NULL);
23
WM_TIMER’s handler nIDEvent –id of the timer which currently send WM_TIMER Ex) afx_msg void OnTimer (int nIDEvent ) void CChildView::OnTimer(int nIDEvent) { if(nIDEvent == 0) { // define any job here } void CChildView::OnTimer(int nIDEvent) { if(nIDEvent == 0) { // define any job here }
24
Stopping a Timer Stop the timer with the given id –id id of the timer(ex: 0, 1, 2, … ) void KillTimer (int id) ; Ex) KillTimer(0);
25
Coding Practice Moving Rectangle Animation 1. Create a variable for the position CPoint m_pt; 2. Draw a rect at the position (OnPaint) dc.Rectangle(m_pt.x, …); 3. Add WM_CREATE handler (OnCreate) 4. Set Timer in OnCreate function SetTimer(0,30,NULL); 5. Add WM_TIMER handler (OnTimer) if(nIDEvent == 0) ….. 1. Create a variable for the position CPoint m_pt; 2. Draw a rect at the position (OnPaint) dc.Rectangle(m_pt.x, …); 3. Add WM_CREATE handler (OnCreate) 4. Set Timer in OnCreate function SetTimer(0,30,NULL); 5. Add WM_TIMER handler (OnTimer) if(nIDEvent == 0) …..
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.