从 .net 向操纵杆发送反馈/效果

2024-02-27

感谢这个答案https://stackoverflow.com/a/13734766/637142 https://stackoverflow.com/a/13734766/637142我能够知道何时按下按钮或何时旋转方向盘。现在我的问题是如何将效果发送到设备?例如,当我玩游戏时,如果我崩溃了,轮子就会振动。怎样才能让方向盘震动?

我相信我需要做的是Start()效果 (http://sharpdx.org/documentation/api/t-sharpdx-directinput-effect http://sharpdx.org/documentation/api/t-sharpdx-directinput-effect)。 SharpDX.DirectInput.Joystick 类似乎没有方法返回所有效果。有一种方法叫做GetEffects但该方法返回一个集合EffectInfo对象。游戏如何向操纵杆发送命令?


源代码是从复制粘贴过来的here http://forums.codeguru.com/showthread.php?434595-Force-feedback-question.

要使用此源,您需要一个“力效应文件" (C:\MyEffectFile.ffe),在操纵杆上“玩”它。

根据要创建“力效果”文件,您需要使用“力编辑器“ 附带DirectX SDK http://www.microsoft.com/en-us/download/details.aspx?id=6812.

(同一本书,或者,声明您可以在代码中从头开始创建效果...在答案中我发现了另一段代码,可以创建和使用效果而不从文件加载它:-))

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;

namespace JoystickProject
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private Device device = null;
        private bool running = true;
        private ArrayList effectList = new ArrayList();
        private bool button0pressed = false;
        private string joyState = "";

        public bool InitializeInput()
        {
            // Create our joystick device
            foreach(DeviceInstance di in Manager.GetDevices(DeviceClass.GameControl,
                EnumDevicesFlags.AttachedOnly | EnumDevicesFlags.ForceFeeback))
            {
                // Pick the first attached joystick we see
                device = new Device(di.InstanceGuid);
                break;
            }
            if (device == null) // We couldn't find a joystick
                return false;

            device.SetDataFormat(DeviceDataFormat.Joystick);
            device.SetCooperativeLevel(this, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Background);
            device.Properties.AxisModeAbsolute = true;
            device.Properties.AutoCenter = false;
            device.Acquire();

            // Enumerate any axes
            foreach(DeviceObjectInstance doi in device.Objects)
            {
                if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
                {
                    // We found an axis, set the range to a max of 10,000
                    device.Properties.SetRange(ParameterHow.ById,
                        doi.ObjectId, new InputRange(-5000, 5000));
                }
            }

            // Load our feedback file
            EffectList effects = null;
            effects = device.GetEffects(@"C:\MyEffectFile.ffe",
                FileEffectsFlags.ModifyIfNeeded);
            foreach(FileEffect fe in effects)
            {
                EffectObject myEffect = new EffectObject(fe.EffectGuid, fe.EffectStruct,
                    device);
                myEffect.Download();
                effectList.Add(myEffect);
            }

            while(running)
            {
                UpdateInputState();
                Application.DoEvents();
            }

            return true;
        }

        private void PlayEffects()
        {
                // See if our effects are playing.
                foreach(EffectObject myEffect in effectList)
                {
                    //if (button0pressed == true)
                    //{
                        //MessageBox.Show("Button Pressed.");
                    //  myEffect.Start(1, EffectStartFlags.NoDownload);
                    //}

                    if (!myEffect.EffectStatus.Playing)
                    {
                        // If not, play them
                        myEffect.Start(1, EffectStartFlags.NoDownload);
                    }
                }
                //button0pressed = true;
        }

        protected override void OnClosed(EventArgs e)
        {
            running = false;
        }

        private void UpdateInputState()
        {
            PlayEffects();

            // Check the joystick state
            JoystickState state = device.CurrentJoystickState;
            device.Poll();
            joyState = "Using JoystickState: \r\n";

            joyState += device.Properties.ProductName;
            joyState += "\n";
            joyState += device.ForceFeedbackState;
            joyState += "\n";
            joyState += state.ToString();

            byte[] buttons = state.GetButtons();
            for(int i = 0; i < buttons.Length; i++)
                joyState += string.Format("Button {0} {1}\r\n", i, buttons[i] != 0 ? "Pressed" : "Not Pressed");

            label1.Text = joyState;

            //if(buttons[0] != 0)
                //button0pressed = true;
            
        }

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(272, 488);
            this.label1.TabIndex = 0;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.SystemColors.ControlText;
            this.ClientSize = new System.Drawing.Size(288, 502);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.label1});
            this.Name = "Form1";
            this.Text = "Joystick Stuff";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            using (Form1 frm = new Form1())
            {
                frm.Show();
                if (!frm.InitializeInput())
                    MessageBox.Show("Couldn't find a joystick.");
            }
        }
    }
}

我刚刚发现here https://web.archive.org/web/20180316115125/http://xboxforums.create.msdn.com/forums/t/92749.aspx另一段可能有用的代码。 该示例似乎是从头开始创建效果的,因此您不需要“效果文件”。

   DeviceList xDeviceList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); 
            DeviceInstance someDeviceInstance; 
            foreach (DeviceInstance deviceInstance in xDeviceList) 
            { 
                someDeviceInstance = deviceInstance; 
                break; 
            } 
 
            Device someDevice = new Device(someDeviceInstance.InstanceGuid); 
            someDevice.SetCooperativeLevel(this.Handle, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Background); 
            int[] axis = new int[0]; 
            foreach (DeviceObjectInstance doi in someDevice.Objects) 
            { 
                if((doi.Flags & (int)ObjectInstanceFlags.Actuator) != 0) 
                { 
                    axis = new int[axis.Length + 1]; 
                    axis[axis.Length - 1] = doi.Offset; 
                } 
            } 
 
            someDevice.Acquire(); 
 
            Effect effect = new Effect(); 
            effect.SetDirection(new int[axis.Length]); 
            effect.SetAxes(new int[axis.Length]); 
            effect.ConditionStruct = new Condition[axis.Length]; 
 
            effect.Flags = EffectFlags.Cartesian | EffectFlags.ObjectOffsets; 
            effect.Duration = int.MaxValue; 
            effect.SamplePeriod = 0; 
            effect.Gain = 10000; 
            effect.TriggerButton = (int)Microsoft.DirectX.DirectInput.Button.NoTrigger; 
            effect.TriggerRepeatInterval = 0; 
            effect.UsesEnvelope = false; 
            effect.EffectType = Microsoft.DirectX.DirectInput.EffectType.ConstantForce; 
            effect.StartDelay = 0; 
            effect.Constant = new Microsoft.DirectX.DirectInput.ConstantForce(); 
            effect.Constant.Magnitude = -5000; 
            EffectObject effectObject = null; 
            foreach (EffectInformation ei in someDevice.GetEffects(EffectType.ConstantForce)) 
            { 
                effectObject = new EffectObject(ei.EffectGuid, effect, someDevice); 
            } 
 
            effectObject.SetParameters(effect, EffectParameterFlags.Start ); 

这是另一个链接,可能会有用力反馈样本 https://www.codeproject.com/Articles/24412/Force-Feedback-in-Managed-DirectX

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 .net 向操纵杆发送反馈/效果 的相关文章

随机推荐

  • CSS 动画延迟不起作用

    尝试淡入一个 div 7 秒后 淡入另一个 div 我一生都无法弄清楚为什么它不起作用 动画本身可以工作 淡入 淡出动画 但我需要 正在进行的 div 在设定的秒数后淡入 有人知道如何正确执行此操作吗 coming width 320px
  • Android:使用 onClick 更改 ListView 行中的按钮背景

    我的行包含一个按钮 该按钮在我的适配器的 getView 中设置了自己的单击侦听器 我可以使用行父级中的 android descendantFocusability blocksDescendants 来区分按钮点击和实际行项目点击 当我
  • 如何从 pandas 的第一个元素开始重新采样?

    我正在对下表 数据进行重新采样 Timestamp L x L y L a R x R y R a 2403950 621 3 461 3 313 623 3 461 8 260 2403954 622 5 461 3 312 623 3
  • Python 文档字符串中的字符串操作

    我一直在尝试做以下事情 def history dependent simulate self node iterations 1 args kwargs For history dependent simulations only sel
  • Windows 版 Git 不执行我的 .bashrc 文件

    我刚刚在 Windows 7 上安装了 Git for Windows 2 5 0 看来我的 bashrc当我运行 Git Bash 时 文件没有被执行 我像这样创建了文件 Administrator HintTech Dev MINGW6
  • 如何在 Visual Studio 2010 中查看二维数组的所有元素?

    我正在 Visual Studio 2010 中调试我的 C 代码 并希望查看数组的内容 例如 Q 它的大小为 17x17 当我插入断点并尝试调试时 我只看到变量 Q 当我将其带到 观看 屏幕并将其重命名为 Q 17 时 我看到下一级 但我
  • 从解析中删除类/列时出现问题

    我试图从解析中删除一些不需要的列 我不断收到以下错误 错误 类名 Session 必须以字母解析开头 不确定为什么会发生这种情况 或者这是否是一个错误 当我删除一个类时 我也会收到此错误 有没有解决的办法 UPDATE 我刚刚尝试过 我能够
  • 如何使用 FFMPEG 驱动程序使 opencv 工作

    我的 linuxbox 上有一个摄像头 它运行良好 ls al dev video crw rw 1 root video 81 0 janv 8 16 13 dev video0 crw rw 1 root video 81 1 janv
  • Android获取JSON键值

    我对解析特定的问题有疑问json细绳 我没有找到任何对我的情况有帮助的东西 我有这个 json AM country name Armenia data 180854 time published 2012 03 30 13 31 39 t
  • 使用 C API 访问 NumPy 数组的视图

    在我用 C 编写的 Python 扩展模块中 我使用以下代码片段将 NumPy 数组转换为犰狳 http arma sourceforge net 用于代码的 C 部分的数组 static arma mat convertPyArrayTo
  • Django 查询集按 ISO 周数过滤

    我有一个模型 其中包含datefield 我正在尝试获取包含本周 从星期一开始 的模型的查询集 所以自从姜戈datefield包含简单的datetime date我假设使用的模型进行过滤 isocalendar 从逻辑上讲 这正是我想要的
  • Date.js parseExact() 当作为数组传入时不解析 4 位数年份

    我是否在 date js 中遗漏了 Date parseExact 的某些内容 根据api文档 我应该能够做到这一点 Date parseExact 10 15 2004 M d yyyy MMMM d yyyy The Date of 1
  • 如何链接 TextView 中的文本以打开网址

    我花了一个多小时查看了大量示例 但没有一个实际上适用于在 TextView 中设置文本以链接到 Web URL 示例代码 text8 TextView findViewById R id textView4 text8 setMovemen
  • 在包含字符串列表的系列上使用 Pandas 字符串方法“包含”

    给定一个简单的 Pandas 系列 其中包含一些可由多个句子组成的字符串 In import pandas as pd s pd Series This is a long text It has multiple sentences Do
  • 将 JSON 对象传递给 MVC 控制器时 string.empty 转换为 null

    我正在将一个对象从客户端传递到服务器 在此过程中 表示为 string empty 的对象属性将转换为 null 我想知道当对象类型支持 string empty 时如何防止这种情况 console log DataToPost dataT
  • 常见爬行-获取WARC文件

    我想使用常见的爬网检索网页 但我迷路了 我想要获取 www example com 的 warc 文件 我看到这个链接 生成以下 json urlkey com example 时间戳 20170820000102 mime text ht
  • 在 Safari iPhone 上的新选项卡中打开链接

    我有一个网站可以帮助人们为 Instagram 帖子创建标题并评估主题标签 其中一项功能非常简单 只需将主题标签链接到 Instagram 即可查看它们包含哪些类型的图像 因为我不希望他们刚刚输入和评估的输入消失 所以我使用 target
  • iOS 获取数组中选定联系人的电子邮件地址

    我想做的就是向用户展示人员选择器 让他选择他想要的所有联系人 最后将所有这些联系人的电子邮件地址放入数组中 最好的办法是只向用户显示带有电子邮件的联系人 到目前为止 我唯一能做的就是向人员选择器提供以下代码 ABPeoplePickerNa
  • 使用 css3 淡入淡出背景图像

    嘿大家 我想知道为什么这行不通 right article boy background transparent url images boxes bg boy jpg left top no repeat width 413px heig
  • 从 .net 向操纵杆发送反馈/效果

    感谢这个答案https stackoverflow com a 13734766 637142 https stackoverflow com a 13734766 637142我能够知道何时按下按钮或何时旋转方向盘 现在我的问题是如何将效