如何使用C ++中的Keybd_Event函数模拟空格键事件 [英] How Do I Simulate A Spacebar Event Using The Keybd_Event Function In C++

查看:786
本文介绍了如何使用C ++中的Keybd_Event函数模拟空格键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用空格键事件暂停一首歌。我想使用keybd_event函数来模拟空格键。我在运行程序之前在后台播放了一个音频文件,如果我的代码有效,那么空格键就会暂停音频文件。但什么都没发生。

我使用的是Windows 8 64位操作系统,我不知道这与我的代码有什么关系,但我还是想提一下。任何建议或更正都会非常有帮助。



这就是我试过的事情



  #include   <   iostream  >  
#include < windows.h >
#include < stdio.h >

使用 命名空间标准;

int main()
{

keybd_event(VK_SPACE, 0 0 0 ); // 按空格键
system( pause);

return 0 ;
}

解决方案

好像你将通过控制键盘输入焦点与Windows争夺战。



运行程序时 - 从命令行或通过Windows资源管理器,键盘焦点移动到用于运行程序的任何窗口。



一个程序启动,作为控制台程序,它将创建自己的窗口并再次获取键盘输入焦点。



更多程序正在向自己发送空格键。



在程序中添加代码,找到要控制的应用程序的顶部窗口,并将焦点设置为其他应用程序 - 在发送空格键之前。



为了进一步减少程序的侵入性,将其重写为WinMain()而不是main。将项目设置为Windowed子系统应用程序而不是控制台应用程序。在WinMain下运行并且从不创建窗口的程序无法获取输入焦点。



一个小建议 - 空格键的扫描代码显示为0x39。可能不需要但不会受到伤害。



http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v = vs.85).aspx [ ^ ]

I want to use the spacebar event to pause a song . I wanted to use the keybd_event function to simulate the spacebar. I played an audio file in the background before running my program and if my code would have worked then the spacebar key would have paused the audio file. But nothing happened.
I am using Windows 8 64bit OS and i dont know if that has anything to do with my code but still i wanted to mention about it. Any suggestions or corrections will be really helpful.

This is what i tried

#include <iostream>
#include <windows.h>
#include<stdio.h>

using namespace std;

int main()
{
	
        keybd_event(VK_SPACE,0,0,0); // Press Spacebar
        system("pause");    

	return 0;
}

解决方案

Seems like you're going to have a fight with windows over control of the keyboard input focus.

When you run your program - from a command line or through windows explorer, the keyboard focus moves to whatever window you used to run the program.

One the program starts, as a console program it will create it's own window and again take the keyboard input focus.

More than likely the program is sending the spacebar to itself.

Add code to your program to locate the top window of the application you want to control and set the focus to the other application - before sending a space bar.

To further reduce the intrusiveness of your program, rewrite it as WinMain() instead of main. Set the project as a Windowed subsystem application instead of a console application. A program that runs under WinMain and never creates a window can't take the input focus.

One minor suggestion - the scan code for the spacebar appears to 0x39. Probably not needed but doesn't hurt.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx[^]


这篇关于如何使用C ++中的Keybd_Event函数模拟空格键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆