📄 tut6.html
字号:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Iczelion">
<meta name="GENERATOR" content="Mozilla/4.51 [en] (Win95; I) [Netscape]">
<title>Iczelion's Win32 Assembly Tutorial 6: Keyboard Input</title>
</head>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#C0C0C0" alink="#C0FFC0">
<center>
<h1>
<font face="Arial,Helvetica"><font color="#999900">Tutorial 6: Keyboard
Input</font></font></h1></center>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>We will
learn how a Windows program receives keyboard input.</font></font></font>
<p><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Download
the example <a href="files/tut06.zip">here</a>.</font></font></font>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=+0>Theory:</font></font></font></h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Since
normally there's only one keyboard in each PC, all running Windows programs
must share it between them. Windows is responsible for sending the key
strokes to the window which has the input focus.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Although
there may be several windows on the screen, only one of them has the input
focus. The window which has input focus is the only one which can receive
key strokes. You can differentiate the window which has input focus from
other windows by looking at the title bar. The title bar of the window
which has input focus is highlighted.</font></font></font>
<br><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>Actually,
there are two main types of keyboard messages, depending on your view of
the keyboard. You can view a keyboard as a collection of keys. In this
case, if you press a key, Windows sends a WM_KEYDOWN message to the window
which has input focus, notifying that a key is pressed. When you release
the key, Windows sends a WM_KEYUP message. You treat a key as a button.
Another way to look at the keyboard is that it's a character input device.
When you press "a" key, Windows sends a WM_CHAR message to the window which
has input focus, telling it that the user sends "a" character to it. In
fact, Windows sends WM_KEYDOWN and WM_KEYUP messages to the window which
has input focus and those messages will be translated to WM_CHAR messages
by TranslateMessage call. The window procedure may decide to process all
three messages or only the messages it's interested in. Most of the time,
you can ignore WM_KEYDOWN and WM_KEYUP since TranslateMessage function
call in the message loop translate WM_KEYDOWN and WM_KEYUP messages to
WM_CHAR messages. We will focus on WM_CHAR in this tutorial.</font></font></font>
<br>
<h3>
<font face="Arial,Helvetica"><font color="#CCCCCC"><font size=+0>Example:</font></font></font></h3>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.386</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.model
flat,stdcall</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>option
casemap:none</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
proto :DWORD,:DWORD,:DWORD,:DWORD</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\windows.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\user32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\kernel32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>include
\masm32\include\gdi32.inc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\user32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\kernel32.lib</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>includelib
\masm32\lib\gdi32.lib</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.data</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>ClassName
db "SimpleWinClass",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>AppName
db "Our First Window",0</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>char
WPARAM 20h
; the character the program receives from keyboard</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.data?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>hInstance
HINSTANCE ?</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>CommandLine
LPSTR ?</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>.code</font></font></font></b>
<br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>start:</font></font></font></b>
<br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke GetModuleHandle, NULL</font></font></font></b> <br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov hInstance,eax</font></font></font></b> <br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke GetCommandLine<br>
mov CommandLine,eax</font></font></font></b><br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</font></font></font></b>
<br>
<b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke ExitProcess,eax</font></font></font></b>
<p><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>WinMain
proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL wc:WNDCLASSEX</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL msg:MSG</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
LOCAL hwnd:HWND</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.cbSize,SIZEOF WNDCLASSEX</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.style, CS_HREDRAW or CS_VREDRAW</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.lpfnWndProc, OFFSET WndProc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.cbClsExtra,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.cbWndExtra,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
push hInst</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
pop wc.hInstance</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hbrBackground,COLOR_WINDOW+1</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.lpszMenuName,NULL</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.lpszClassName,OFFSET ClassName</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke LoadIcon,NULL,IDI_APPLICATION</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hIcon,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hIconSm,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke LoadCursor,NULL,IDC_ARROW</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
mov wc.hCursor,eax</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke RegisterClassEx, addr wc</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</font></font></font></b>
<br><b><font face="Arial,Helvetica"><font color="#CCCCCC"><font size=-1>
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\</font></font></font></b>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -