将左鼠标按钮向上发送后,检测左键按钮
Detect left mouse button down after sending left mouse button up
我正在编写一个程序,如果按下左鼠标按钮,则扫描,然后将左鼠标按钮向上发送并在此之后继续。问题是,由于我要发送一个左鼠标按钮,因此程序将不会继续,因为左鼠标按钮不再被固定。
这是一些伪:
if(GetKeyState(VK_LBUTTON) < 0){
Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
//Rest of code
}
在此之后,如何检测左鼠标按钮被下降?我必须使用驱动程序吗?
只使用标志:
bool lButtonWasDown=flase;
if(GetKeyState(VK_LBUTTON) < 0){
Sleep(10);
lButtonWasDown = true;
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
}
if (lButtonWasDown)
{
// rest of the code for lButtonWasDown = true
}
从这里阅读您对程序的描述是我的实现。使用Windows API:
while (true) {
//check if left mouse button is down
if (GetKeyState(VK_LBUTTON) & 0x8000) {
//send left mouse button up
//You might want to place a delay in here
//to simulate the natural speed of a mouse click
//Sleep(140);
INPUT Input = { 0 };
::ZeroMemory(&Input, sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1, &Input, sizeof(INPUT));
}
}
,如果您想在强制阻止人们单击和拖动时,可以将此代码放入线程中调用的函数。
void noHoldingAllowed() {
//insert code here used above...
}
int main(void) {
std::thread t1(noHoldingAllowed);
//other stuff...
return 0;
{
我尚未测试此代码,但应该起作用
while(programIsRunning)
{
if(GetKeyState(VK_LBUTTON) < 0){
Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
// rest of the code
}
它应该可以工作,因为如果您在循环重新运行时持有LMouse按钮,则会触发IF语句,然后触发鼠标UP事件。
注意:您可能可以做while(GetKeyState(VK_LBUTTON) < 0){//code here}
相关文章:
- 使用CMake检测支持的C++标准
- Winapi 检测按钮悬停
- QT - 检测按下了哪个按钮
- 如何实现一致的方法来检测鼠标按钮被按住
- 单击功能区按钮时检测当前打开MailItem
- 将左鼠标按钮向上发送后,检测左键按钮
- 铬嵌入式检测按钮按下
- 检测笔记本电脑上的电源按钮按下
- 如何检测鼠标右键点击按钮
- 如何检测是否在Qt中同时按下了两个鼠标按钮
- 正在检测何时单击按钮?[C++,WinAPI]
- 是否可以在 MFC 按钮控件上检测到鼠标右键单击事件
- 如何检测全局鼠标按钮事件
- 检测单击的多个按钮的最佳方法是什么
- BN_CLICKED用于检测菜单和按钮点击还是仅用于按钮点击
- 检测鼠标按钮是否按下
- 如何从qtablewidget检测单元格中单击的按钮
- 我如何在QWizard中检测后退按钮被按下?
- Visual c++检测右键单击按钮
- Web浏览器控件中的c++:检测鼠标点击特定按钮