Wednesday, August 4, 2010

Handle keyboard input message


In this lesson, we will learn how to handle the keyboard WINDOWS procedure messages.

Theory:

Because most of the PC is only a keyboard, so all the running in the WINDOWS program Bixugongyong it. WINDOWS will be responsible for the keystroke message sent with the input focus to that application. Although the screen may also have several application windows, but only one window at a time with the input focus. Have the input focus of that application's caption is always high brightness display. In fact you can look at the keyboard message from two perspectives: First, you can see it as a collection of a lot of key information, in this case, when you press a key, WINDOWS will send a WM_KEYDOWN to have the input focus of that application, to remind it has a key is pressed. When you release the key, WINDOWS WM_KYEUP will send a message, telling a key is released. Each key as you put a button; the other is: you can put the keyboard as a character input device. When you press "a" key, WINDOWS send a message to have the input focus WM_CHAR the application, tell it "a" key is pressed. In fact WINDOWS send WM_KEYDOWN and WWM_KEYUP internal message to the application has input focus, and these messages will be translated into WM_CHAR by calling TranslateMessage news. WINDOWS window procedure function to decide whether the message received, in general you are not the General Assembly to deal with WM_KEYDOWN, WM_KEYUP message TranslateMessage function in the message loop will WM_CHAR message into the message. In our program will only handle WM_CHAR.

Examples:

.386
. Model flat, stdcall
option casemap: none
WinMain proto: DWORD,: DWORD,: DWORD,: DWORD

include masm32includewindows.inc
include masm32includeuser32.inc
include masm32includekernel32.inc
include masm32includegdi32.inc
includelib masm32libuser32.lib
includelib masm32libkernel32.lib
includelib masm32libgdi32.lib

. Data
ClassName db "SimpleWinClass", 0
AppName db "Our First Window", 0
char WPARAM 20h; the character the program receives from keyboard

. Data?
hInstance HINSTANCE?
CommandLine LPSTR?

. Code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke GetCommandLine
mov CommandLine, eax
invoke WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
invoke ExitProcess, eax

WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD
LOCAL wc: WNDCLASSEX
LOCAL msg: MSG
LOCAL hwnd: HWND
mov wc.cbSize, SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra, NULL
mov wc.cbWndExtra, NULL
push hInst
pop wc.hInstance
mov wc.hbrBackground, COLOR_WINDOW +1
mov wc.lpszMenuName, NULL
mov wc.lpszClassName, OFFSET ClassName
invoke LoadIcon, NULL, IDI_APPLICATION
mov wc.hIcon, eax
mov wc.hIconSm, eax
invoke LoadCursor, NULL, IDC_ARROW
mov wc.hCursor, eax
invoke RegisterClassEx, addr wc
invoke CreateWindowEx, NULL, ADDR ClassName, ADDR AppName,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hInst, NULL
mov hwnd, eax
invoke ShowWindow, hwnd, SW_SHOWNORMAL
invoke UpdateWindow, hwnd
. WHILE TRUE
invoke GetMessage, ADDR msg, NULL, 0,0
. BREAK. IF (! Eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
. ENDW
mov eax, msg.wParam
ret
WinMain endp

WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM
LOCAL hdc: HDC
LOCAL ps: PAINTSTRUCT

. IF uMsg == WM_DESTROY
invoke PostQuitMessage, NULL
. ELSEIF uMsg == WM_CHAR
push wParam
pop char
invoke InvalidateRect, hWnd, NULL, TRUE
. ELSEIF uMsg == WM_PAINT
invoke BeginPaint, hWnd, ADDR ps
mov hdc, eax
invoke TextOut, hdc, 0,0, ADDR char, 1
invoke EndPaint, hWnd, ADDR ps
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

Analysis:

char WPARAM 20h; the character the program receives from keyboard

This variable will be saved to the character received from the keyboard. Because it is in the process window through WPARAM variable transmission, so we simply define it as type WPARAM. Since our initial refresh the window (that has just been created that a) there is no keyboard input so we put him in as the space character (20h), it appears you did not see anything.

. ELSEIF uMsg == WM_CHAR
push wParam
pop char
invoke InvalidateRect, hWnd, NULL, TRUE
This section is used to handle WM_CHAR messages. It is the received characters into the variable char in, then call InvalidateRect, but InvalidateRect make the window client area is invalid, so that it will issue a WM_PAINT message, the force WM_PAINT messages to its customers WINDOWS redrawn district. The function syntax is as follows:

InvalidateRect proto hWnd: HWND,
lpRect: DWORD,
bErase: DWORD

lpRect is the point we want the client area of a square structure invalid pointer.濡傛灉璇ュ?绛変簬NULL锛屽垯鏁翠釜瀹㈡埛鍖洪兘鏃犳晥锛涘竷灏斿?bErase鍛婅瘔WINDOWS鏄惁鎿﹂櫎鑳屾櫙锛屽鏋滄槸TRUE锛屽垯WINDOWS鍦ㄨ皟鐢˙eginPaint鍑芥暟鏃舵妸鑳屾櫙鎿︽帀銆傛墍浠ユ垜浠澶勭殑鍋氭硶鏄細鎴戜滑灏嗕繚瀛樻墍鏈夋湁鍏抽噸缁樺鎴峰尯鐨勬暟鎹紝鐒跺悗鍙戦?WM_PAINT娑堟伅锛屽鐞嗚娑堟伅鐨勭▼搴忔鐒跺悗鏍规嵁鐩稿叧鏁版嵁閲嶆柊缁樺埗瀹㈡埛鍖恒?灏界杩欎箞鍋氫簨鏈夌偣鍍忚蛋浜嗗紦鑳岋紝浣哤INDOWS瑕佸鐞嗛偅涔堝簽澶х殑娑堟伅缇わ紝娌℃湁涓?畾鐨勮鐭╁彲涓嶈銆傚疄闄呬笂鎴戜滑瀹屽叏鍙互閫氳繃璋冪敤GetDC 鑾峰緱璁惧涓婁笅鏂囧彞鏌勶紝鐒跺悗缁樺埗瀛楃锛岀劧鍚庡啀璋冪敤ReleaseDC閲婃斁璁惧涓婁笅鏂囧彞鏌勶紝姣棤鐤戦棶杩欐牱涔熻兘鍦ㄥ鎴峰尯缁樺埗鍑烘纭殑瀛楃銆備絾鏄鏋滆繖涔嬪悗鎺ユ敹鍒癢M_PAINT娑堟伅瑕佸鐞嗘椂锛屽鎴峰尯浼氶噸鏂板埛鏂帮紝鑰屾垜浠繖绋嶅墠鎵?粯鍒剁殑瀛楃灏变細娑堝け鎺夈?鎵?互涓轰簡璁╁瓧绗︿竴鐩存纭湴鏄剧ず锛屽氨蹇呴』鎶婂畠浠斁鍒癢M_PAINT鐨勫鐞嗚繃绋嬩腑澶勭悊銆傝?鍦ㄦ湰娑堟伅澶勭悊涓彂閫乄M_PAINT娑堟伅鍗冲彲銆?br />
invoke TextOut,hdc,0,0,ADDR char,1

鍦ㄨ皟鐢↖nvalidateRect鏃讹紝WM_PAINT娑堟伅琚彂閫佸埌浜哤INDOWS绐楀彛澶勭悊杩囩▼锛岀▼搴忔祦绋嬭浆绉诲埌澶勭悊WM_PAINT娑堟伅鐨勭▼搴忔锛岀劧鍚庤皟鐢˙eginPaint寰楀埌璁惧涓婁笅鏂囩殑鍙ユ焺锛屽啀璋冪敤TextOut鍦ㄥ鎴峰尯鐨勶紙0锛?锛夊杈撳嚭淇濆瓨鐨勬寜閿瓧绗︺?杩欐牱鏃犺鎮ㄦ寜浠?箞閿兘鑳藉湪瀹㈡埛鍖虹殑宸︿笂瑙掓樉绀猴紝涓嶄粎濡傛锛屾棤璁烘偍鎬庝箞缂╂斁绐楀彛锛堣揩浣縒INDOWS閲嶆柊缁樺埗瀹冪殑瀹㈡埛鍖猴級锛屽瓧绗﹂兘浼氬湪姝g‘鐨勫湴鏂规樉绀猴紝鎵?互蹇呴』鎶婃墍鏈夐噸瑕佺殑缁樺埗鍔ㄤ綔閮芥斁鍒板鐞哤M_PAINT娑堟伅鐨勭▼搴忔涓幓銆?br />





相关链接:



Mkv To Vob



Holiday Birth to "steal food hand job"



Premier Kids Education



Suspected Of Extortion Donews Producer Liu Ren Denies Allegations



convert avi To wmv free download



JSP variable in the scope of the specification



HP, The "total"



New XML Or CSS Tools



Log Analysers Comparison



Alibaba CEO Wei Zhe: mergers and acquisitions in the domestic launch



Ps3 rmvb



Experts say too early to lower domestic oil Prices



avi converter TO mp4



Barbed words! Photoshop special effects make the word cactus