user-0 提问: vs2012用什么函数接收键盘的相应？

回复:
user: user-1
content: #include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }

user: user-1
content: #include <conio.h> #include <windows.h>  void ConPrintAt(int x, int y, char *CharBuffer, int len) {    DWORD count;    COORD coord = {x, y};    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    SetConsoleCursorPosition(hStdOut, coord);    WriteConsole(hStdOut, CharBuffer, len, &count, NULL); } void HideTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = FALSE;       SetConsoleCursorInfo(hStdOut, &cciCursor);    } }  void ShowTheCursor() {    CONSOLE_CURSOR_INFO cciCursor;    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     if(GetConsoleCursorInfo(hStdOut, &cciCursor))    {       cciCursor.bVisible = TRUE;       SetConsoleCursorInfo(hStdOut, &cciCursor);    } } void GetWH(int *w,int *h) {     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);     CONSOLE_SCREEN_BUFFER_INFO csbi;     if (GetConsoleScreenBufferInfo(hStdOut, &csbi)) {         *w=csbi.srWindow.Right;         *h=csbi.srWindow.Bottom;     } else {         *w=80;         *h=25;     } } void ClearConsole() {    //Get the handle to the current output buffer...    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);    //This is used to reset the carat/cursor to the top left.    COORD coord = {0, 0};    //A return value... indicating how many chars were written    //   not used but we need to capture this since it will be    //   written anyway (passing NULL causes an access violation).    DWORD count;    //This is a structure containing all of the console info    // it is used here to find the size of the console.    CONSOLE_SCREEN_BUFFER_INFO csbi;    //Here we will set the current color    if(GetConsoleScreenBufferInfo(hStdOut, &csbi))    {       //This fills the buffer with a given character (in this case 32=space).       FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);       //This will set our cursor position for the next print statement.       SetConsoleCursorPosition(hStdOut, coord);    } }  //This will set the position of the cursor int main() {     unsigned short k;     int x,y,w,h;  	SetConsoleOutputCP(437);     ClearConsole();     GetWH(&w,&h);     x=w/2;y=h/2;     HideTheCursor();     ConPrintAt(x,y,"\xdb",1);     while (1) {         Sleep(50);         k=getch();         if (27==k) break;//按Esc键退出         if (0==k||0xe0==k) k|=getch()<<8;//非字符键         switch (k) {             case 0x48e0:case 0x04800://上                 if (y>0) {                     ConPrintAt(x,y," ",1);                     y--;                     ConPrintAt(x,y,"\xdb",1);                 }             break;             case 0x50e0:case 0x05000://下                 if (y<h) {                     ConPrintAt(x,y," ",1);                     y++;                     ConPrintAt(x,y,"\xdb",1);                 }             break;             case 0x4be0:case 0x04b00://左                 if (x>0) {                     ConPrintAt(x,y," ",1);                     x--;                     ConPrintAt(x,y,"\xdb",1);                 }             break;             case 0x4de0:case 0x04d00://右                 if (x<w-1) {                     ConPrintAt(x,y," ",1);                     x++;                     ConPrintAt(x,y,"\xdb",1);                 }             break;         } //      cprintf("%04x pressed.\r\n",k);      }     ClearConsole();     ShowTheCursor();     return 0; }

user: user-2
content: 如果是MFC，则就是，keydown事件。 如果不是MFC，那就用getch这个函数。

user: user-1
content: 引用 9 楼 fd214333890 的回复:[quote=引用 6 楼 zhao4zhong1 的回复:] [quote=引用 4 楼 fd214333890 的回复:] [quote=引用 3 楼 zhao4zhong1 的回复:] #include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }  我怎么用vs那个kbhit()感觉没用啊[/quote] 你是用于Console项目吗？[/quote] 用的是win32应用程序。[/quote] Win32需要处理WM_KEYUP消息。 WM_KEYUP The WM_KEYUP message is posted to the window with the keyboard focus when a nonsystem key is released. A nonsystem key is a key that is pressed when the alt key is not pressed, or a keyboard key that is pressed when a window has the keyboard focus.   WM_KEYUP  nVirtKey = (int) wParam;    // virtual-key code  lKeyData = lParam;          // key data    Parameters nVirtKey  Value of wParam. Specifies the virtual-key code of the nonsystem key.  lKeyData  Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. Value Description  0–15 Specifies the repeat count for the current message. The value is the number of times the keystroke is auto-repeated as a result of the user holding down the key. The repeat count is always one for a WM_KEYUP message.  16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).  24 Specifies whether the key is an extended key, such as the right-hand alt and ctrl keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.  25–28 Reserved; do not use.  29 Specifies the context code. The value is always 0 for a WM_KEYUP message.  30 Specifies the previous key state. The value is always 1 for a WM_KEYUP message.  31 Specifies the transition state. The value is always 1 for a WM_KEYUP message.    Return Values An application should return zero if it processes this message.   Default Action The DefWindowProc function sends a WM_SYSCOMMAND message to the top-level window if the f10 key or the alt key was released. The wParam parameter of the message is set to SC_KEYMENU.   Remarks For enhanced 101- and 102-key keyboards, extended keys are the right alt and ctrl keys on the main section of the keyboard; the ins, del, home, end, page up, page down and arrow keys in the clusters to the left of the numeric keypad; and the divide (/) and enter keys in the numeric keypad. Other keyboards may support the extended-key bit in the lKeyData parameter.   QuickInfo   Windows NT: Requires version 3.1 or later.   Windows: Requires Windows 95 or later.   Windows CE: Requires version 1.0 or later.   Header: Declared in winuser.h.  See Also Keyboard Input Overview, Keyboard Input Messages, DefWindowProc, WM_KEYDOWN, WM_SYSCOMMAND

user: user-3
content: hook  WH_KEYBOARD_LL

user: user-2
content: 引用 9 楼 fd214333890 的回复:[quote=引用 6 楼 zhao4zhong1 的回复:] [quote=引用 4 楼 fd214333890 的回复:] [quote=引用 3 楼 zhao4zhong1 的回复:] #include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }  我怎么用vs那个kbhit()感觉没用啊[/quote] 你是用于Console项目吗？[/quote] 用的是win32应用程序。[/quote] The _kbhit function checks the console for a recent keystroke.  这个只能在console 里面用。。。

user: user-0
content: 引用 6 楼 zhao4zhong1 的回复:[quote=引用 4 楼 fd214333890 的回复:] [quote=引用 3 楼 zhao4zhong1 的回复:] #include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }  我怎么用vs那个kbhit()感觉没用啊[/quote] 你是用于Console项目吗？[/quote] 用的是win32应用程序。

user: user-4
content: windowshttp://baike.baidu.com/link?url=LK3Ulx3OP0ZOYG7jhg82U4afQCGsarhS0GPGIGEIKtC9UjlxQxtCdITSy8gshngNpdeIFQ6BITqjCxjGnWO1q_

user: user-2
content: The _kbhit function checks the console for a recent keystroke. If the function returns a nonzero value, a keystroke is waiting in the buffer. The program can then call _getch or _getche to get the keystroke. ------------------------------------- _kbhit函数，检查控制台最近的一个按键状态，如果这个函数返回非0值，则输入缓冲区里面有一个按键信息等待读取，可以用_getch或getche函数读取按键信息。

user: user-1
content: 引用 4 楼 fd214333890 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] #include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }  我怎么用vs那个kbhit()感觉没用啊[/quote] 你是用于Console项目吗？

user: user-2
content: 引用 楼主 fd214333890 的回复:我再做一个游戏，因为要用到键盘，所以需要键盘的消息。但是我用#include<conio.h>里的_kbhit()不行啊。。。连点反应都没有。。。。有没有人知道该用什么？ MSDN里面的例子。 不断的输出一段东西。知道按任意键，停止。   #include <conio.h> #include <stdio.h>  void main( void ) {    /* Display message until key is pressed. */    while( !_kbhit() )       _cputs( "Hit me!! " );     /* Use _getch to throw key away. */    printf( "\nKey struck was '%c'\n", _getch() );    _getch(); }

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:#include <conio.h> #include <windows.h> int main() {     int k;      while (1) {         if (kbhit()) {             k=getch();             if (0==k || 0xE0==k) k=k<<8|getch();             if (27==k) break;//按Esc键退出             cprintf("\r\n%04X\r\n",k);         }         Sleep(200);         cprintf(".");     }     return 0; }  我怎么用vs那个kbhit()感觉没用啊