轮询操纵杆 C#

发布于 2024-07-16 10:06:23 字数 491 浏览 8 评论 0原文

我有一个应用程序,可以记录按下空格键时的日期/时间 不幸的是,由于各种原因,窗口并不总是具有焦点。 例如,病毒检查器弹出窗口、itunes 更新、Windows 更新等,

所以我认为一个很酷的想法是使用操纵杆按钮,然后无论哪个窗口具有焦点,我始终可以检测到按钮按下事件并记录日期/时间它被压了。

现在我看了一些游戏教程,但他们似乎将操纵杆轮询与屏幕刷新事件联系在一起,因为在游戏中我想如果计算机很慢并且屏幕不经常刷新,则有操纵杆按钮无论如何,按下不会有什么区别。

这并不真正适合我的需要,所以我正在考虑运行一个线程来经常轮询操纵杆

我的问题是

  1. 后台线程最好的解决方案
  2. 我应该多久轮询操纵杆
  3. 我应该使用什么样的计时器确定轮询频率。

我希望响应至少相当于键盘。 还需要确保按住扳机时不会自动重复。

任何示例代码 (C#)

感谢

alex

i have an application that records a date/time when the space bar is pressed
unfortunately, the window doesn't always have focus, for various reasons. eg virus checker popup, itunes update, windows update, etc

so what i though would be a cool idea is to use a joystick button, then regardless of which window has focus, i can always detect the button press event and record the date/time it was pressed.

now i had a look at a few game tutorials, but they seem to have the joystick polling tied into the screen refresh event, because in a game i guess if the computer is slow and the screen doesn't refresh often, having the joystick button pressed is going to make no difference anyway.

That doesn't really suit my need, so i am looking at running a thread that will poll the joystick every so often

My questions are

  1. is a background thread the best solution
  2. how often should i poll the joystick
  3. what kind of timer should i use to determine the polling frequency.

I would like the responses to be at least equivalent to a keyboard. also need to make sure it doesn't auto-repeat if trigger is held down.

any sample code (C#) appreciated

thanks

alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-07-23 10:06:23

如果您只是因为无法捕获键盘事件而想要轮询操纵杆,我认为您需要重新制定策略。

即使您的表单没有焦点,也可以通过一些方法确保您了解键盘状态。

  1. 您可以设置一个系统范围的键盘挂钩,可以使用 SetWindowsHookEx API 调用在 C# 中完成。

这是对非托管代码和 Windows API 的深入研究,但我认为这比轮询操纵杆更好,因为您不需要仅仅因为需要在按下空格键时捕获日期时间而向计算机添加操纵杆。

在 google 上搜索一下 C# 中的键盘挂钩。 这比许多人想象的要容易。

我认为使用计时器对象来执行此操作根本不是一个好主意。 因为我相信你说过你需要按一个键来完成它。

If you want to go for polling the joystick just because you are not able to capture the keyboard event, I think you need to re-work your strategy.

Even if your form doesn't have the focus there are ways that you can make sure you know of the keyboard state.

  1. You could set up a system wide keyboard hook which can be done in C# using the SetWindowsHookEx API call.

This is delving a little in unmanaged code and Windows API, but I think it's a better idea than polling a joystick cause you won't need to add a joystick to your computer just because you need to capture datetime at the press of a spacebar.

Do a little search on google for keyboard hooks in C#. It's easier than many people believe.

I don't think using a timer object to do this is a good idea at all. Cause I believe you said you need to do it at the press of a key.

美人迟暮 2024-07-23 10:06:23

最好的选择是在窗体上创建一个 Timer 对象,并将操纵杆检查与 Tick 事件联系起来。

我还发现,每 100 毫秒(每秒 10 次)运行一次这样的简单操作对 CPU 使用率的影响可以忽略不计。

Your best bet would be to create a Timer object on your form, and tie the joystick check to the Tick event.

I've also found that running simple stuff like this every 100ms (10 times a second) has a negligible effect on CPU usage.

哀由 2024-07-23 10:06:23

使用类似 AHK (Auto HotKey) 的语言,它是一种简单的语言,可以编译为 EXE,并且被设计为对于自动化键盘和鼠标,我不能 100% 确定它与操纵杆的配合效果如何,但我确信他们可能设法为 Wii 遥控器编写一些东西。

此外,IRC 频道和论坛总是有人愿意在需要时提供帮助。

Use something like AHK (Auto HotKey) it is a simple language that can be compiled to an EXE and is designed for automating the keyboard and mouse, I'm not 100% sure how well it works with a joystick, but I'm sure it's possible they managed to write some stuff for the wii remotes.

Also the IRC Channel and Forums always have people willing to help if need be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文