创建游戏杆事件? [英] Creating A Joystick Event?

查看:124
本文介绍了创建游戏杆事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我到处都有搜索.我正在尝试创建类似于鼠标单击等的操纵杆事件,但我只能找到操纵杆轮询(使用计时器检查操纵杆的状态.)

我正在使用vb.net和DirectX输入.是否可以创建一个类,该类可以创建在状态更改时触发的操纵杆事件?这似乎是一种可能性,但我却难以捉摸.

谢谢! :)

Hello all,

I have search everywhere. I am trying to create a joystick event similar to a mouse click etc but all I can find is joystick polling (using a timer to check the state of the joystick.)

I am using vb.net and DirectX input. Is it possible to create a class that can create a joystick event that fires upon state change? It seems like a possibility but it eludes me.

Thanks! :)

推荐答案

用于操纵杆工作的DirectX输入是另一种方式.您可以创建一个Event(.NET API为EventWaitHandle)对象,该对象由操纵杆驱动程序中断在每个输入事件上设置.您可以编写引发事件的代码.

方法如下:创建输入事件对象.创建一个线程;在该线程中进行一个无限循环,等待该事件对象.在等待期间,线程被设置为等待状态,并消耗零的CPU时间,直到被事件等待句柄唤醒为止.发生这种情况时,请重新读取操纵杆状态,将状态更改(与之前的状态相比)打包到某个事件参数中,然后触发该事件;然后循环将使它再次处于等待状态.这样,该线程将用作事件源.总体而言,它看起来像是中断驱动的行为.

我只有C#代码,因此可以使用Microsoft.DirectX.DirectInput.做这样的事情:

DirectX input for joystick work is a different way. You can create an Event (EventWaitHandle for .NET API) object which is set by the joystick driver interrupt on every input event. You can write code which fires events.

Here is how: Create the input event object. Create a thread; in this thread make an infinite loop which waits for this event object. While waiting, your thread is set to the wait state and consume zero CPU time until awaken by the event wait handle. When it happens, re-read joystick status, pack the change in status (compared to the previous state) into some event argument and fire the event; then the loop will get it the the wait state again. In this way, this thread will work as a source of events. Overall, it looks like interrupt-driven behavior.

I have only C# code, so I can use Microsoft.DirectX.DirectInput. Do something like this:

using Microsoft.DirectX.DirectInput;

JoystickDevice Device;
//Find your device using FindControllers, you can use them all

Consider you have some device wrapper JoystickDevice:

class JoystickDevice {
    public void Acquire(System.Threading.WaitHandle deviceEvent) {
            Device.SetEventNotification(deviceEvent);
            Device.Acquire();
    } //Acquire
}

//this is the key: create another class, thread wrapper, create a some wait handle and pass it
//to Device.Acquire. Now this handle will be Set by hardware device, your thread only waits:
//In this thread:

while (true) {
    JoystickStateNotifierEvent.WaitOne();
    //now poll Joystick state using Device;
    //determine what changed and fire some event
} //loop



—SA



—SA


这篇关于创建游戏杆事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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