windows应用程序设计怎样写同时按下Ctrl和A的情况

致物软件 2014-05-25 03:59:58
VC++编写的win32 Application 程序,怎样做到"当单击Ctrl+A键时, 窗口中显示”CTRL+A Key?
我理解的是按下Ctrl和A
但题目还有个要求是“当单击Ctrl键时, 窗口中显示”CTRL Key”,我写的对应代码是
case WM_KEYDOWN:
if(wParam==VK_CONTROL)
MessageBox(hWnd,"CTRL KEY",NULL,MB_OK);
break;

然后对于Ctrl+A的情况,我是这样写的
case WM_SYSKEYDOWN:
if(wParam==VK_CONTROL&&wParam=='A')
MessageBox(hWnd,"CTRL+A KEY",NULL,MB_OK);
break;

但是每次一按Ctrl就直接弹出”CTRL KEY“,按A没有任何反应我想知道是哪里的问题?
...全文
227 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
和我赛跑的人 2014-05-26
  • 打赏
  • 举报
回复
RegisterHotKey WM_HOTKEY
赵4老师 2014-05-26
  • 打赏
  • 举报
回复
GetAsyncKeyState The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. SHORT GetAsyncKeyState( int vKey // virtual-key code ); Parameters vKey Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes. Windows NT: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information. Return Values If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus. Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero. Remarks The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON) which returns TRUE if the mouse buttons have been swapped. You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right. Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys. Code Meaning VK_LSHIFT VK_RSHIFT VK_LCONTROL VK_RCONTROL VK_LMENU VK_RMENU These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. Windows CE: The GetAsyncKeyState function supports the left and right virtual key constants, so you can determine whether the left key or the right key was pressed. These constants are VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, VK_LSHIFT, and VK_RSHIFT. The least significant bit of the return value is not valid in Windows CE, and should be ignored. GetAsyncKeyState will return the current key state even if a window in another thread or process currently has the keyboard focus. You can also use the VK_LBUTTON virtual-key code constant to determine the state of the stylus tip, (up/down), on the touch-screen. 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. Import Library: Use user32.lib. See Also Keyboard Input Overview, Keyboard Input Functions, GetKeyboardState, GetKeyState,GetSystemMetrics, MapVirtualKey, SetKeyboardState
致物软件 2014-05-26
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
GetAsyncKeyState The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. SHORT GetAsyncKeyState( int vKey // virtual-key code ); Parameters vKey Specifies one of 256 possible virtual-key codes. For more information, see Virtual-Key Codes. Windows NT: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information. Return Values If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus. Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero. Remarks The GetAsyncKeyState function works with mouse buttons. However, it checks on the state of the physical mouse buttons, not on the logical mouse buttons that the physical buttons are mapped to. For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button. You can determine the system's current mapping of physical mouse buttons to logical mouse buttons by calling GetSystemMetrics(SM_SWAPBUTTON) which returns TRUE if the mouse buttons have been swapped. You can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the vKey parameter. This gives the state of the shift, ctrl, or alt keys without distinguishing between left and right. Windows NT: You can use the following virtual-key code constants as values for vKey to distinguish between the left and right instances of those keys. Code Meaning VK_LSHIFT VK_RSHIFT VK_LCONTROL VK_RCONTROL VK_LMENU VK_RMENU These left- and right-distinguishing constants are only available when you call the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions. Windows CE: The GetAsyncKeyState function supports the left and right virtual key constants, so you can determine whether the left key or the right key was pressed. These constants are VK_LCONTROL, VK_RCONTROL, VK_LMENU, VK_RMENU, VK_LSHIFT, and VK_RSHIFT. The least significant bit of the return value is not valid in Windows CE, and should be ignored. GetAsyncKeyState will return the current key state even if a window in another thread or process currently has the keyboard focus. You can also use the VK_LBUTTON virtual-key code constant to determine the state of the stylus tip, (up/down), on the touch-screen. 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. Import Library: Use user32.lib. See Also Keyboard Input Overview, Keyboard Input Functions, GetKeyboardState, GetKeyState,GetSystemMetrics, MapVirtualKey, SetKeyboardState
使用GetAsyncKeyState函数可以解决Ctrl+A情况,但是必须同时按下Ctrl和A,不能有时差.我的代码如下:
case WM_KEYDOWN:
			if(wParam==VK_UP){
				InvalidateRect(hWnd,NULL,true);
				index	= 2;
			}
			else if(wParam==VK_CONTROL){
				InvalidateRect(hWnd,NULL,true);
				index	= 3;
			}
			else if(GetAsyncKeyState(VK_CONTROL)&&wParam=='A'){
				InvalidateRect(hWnd,NULL,true);
				index	= 4;
			}
			break;
致物软件 2014-05-25
  • 打赏
  • 举报
回复
引用 1 楼 u013797412 的回复:
加个static 的BOOL 变量标志下 ctrl键是被按下了,还是弹起了 static BOOL ctrldownflag = FALSE; case WM_KEYDOWN: if(wParam==VK_CONTROL) ctrldownflag = TRUE; if(wParam==0x41 && ctrldownflag) MessageBox(hwnd, TEXT("你按了Ctrl + A"), "", MB_OK); return 0; case WM_KEYUP: if(wParam==VK_CONTROL) ctrldownflag = FALSE; return 0;
不行啊,依旧是按下Ctrl键显示“Ctrl KEY”,同时按下Ctrl和A还是显示“Ctrl KEY”
yanasdf789 2014-05-25
  • 打赏
  • 举报
回复
学习了 我也遇到这个问题
initgraph 2014-05-25
  • 打赏
  • 举报
回复
加个static 的BOOL 变量标志下 ctrl键是被按下了,还是弹起了 static BOOL ctrldownflag = FALSE; case WM_KEYDOWN: if(wParam==VK_CONTROL) ctrldownflag = TRUE; if(wParam==0x41 && ctrldownflag) MessageBox(hwnd, TEXT("你按了Ctrl + A"), "", MB_OK); return 0; case WM_KEYUP: if(wParam==VK_CONTROL) ctrldownflag = FALSE; return 0;

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧