当前位置: 首页>>代码示例>>C#>>正文


C# Joystick.Acquire方法代码示例

本文整理汇总了C#中Joystick.Acquire方法的典型用法代码示例。如果您正苦于以下问题:C# Joystick.Acquire方法的具体用法?C# Joystick.Acquire怎么用?C# Joystick.Acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Joystick的用法示例。


在下文中一共展示了Joystick.Acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CaptureJoysticks

        public void CaptureJoysticks()
        {
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find all joysticks connected to the system
            IList<DeviceInstance> connectedJoysticks = new List<DeviceInstance>();
            // - look for gamepads
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
            {
                connectedJoysticks.Add(deviceInstance);
            }
            // - look for joysticks
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
            {
                connectedJoysticks.Add(deviceInstance);
            }

            // Use the two first joysticks found
            if (connectedJoysticks.Count >= 1)
            {
                joystick1 = new Joystick(directInput, connectedJoysticks[0].InstanceGuid);
                Joystick1DeviceName = connectedJoysticks[0].InstanceName;
                joystick1.Acquire();
            }
            if (connectedJoysticks.Count >= 2)
            {
                joystick2 = new Joystick(directInput, connectedJoysticks[1].InstanceGuid);
                Joystick2DeviceName = connectedJoysticks[1].InstanceName;
                joystick2.Acquire();
            }
        }
开发者ID:laurentprudhon,项目名称:ZxSpectrumSimulator,代码行数:32,代码来源:JoystickAdapter.cs

示例2: HandleJoystick

        private static void HandleJoystick()
        {
            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                        DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

            // If Gamepad not found, look for a Joystick
            if (joystickGuid == Guid.Empty)
                foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
                        DeviceEnumerationFlags.AllDevices))
                    joystickGuid = deviceInstance.InstanceGuid;

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Debug.WriteLine("No Gamepad found.");
                Environment.Exit(1);
            }

            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Debug.WriteLine("Found Gamepad with GUID: {0}", joystickGuid);

            // Set BufferSize in order to use buffered data.
            joystick.Properties.BufferSize = 128;

            // Acquire the joystick
            joystick.Acquire();

            // Poll events from joystick every 1 millisecond
            var timer = Observable.Interval(TimeSpan.FromMilliseconds(1));

            timer
                // Get all joystick input
                .SelectMany(_ => { joystick.Poll(); return joystick.GetBufferedData(); })
                // Filter only menu key button (xbox one controller)
                .Where(a => a.Offset == JoystickOffset.Buttons6)
                // Input will contain UP and Down button events
                .Buffer(2)
                // If button was pressed longer than a second
                .Where(t => (TimeSpan.FromMilliseconds(t.Last().Timestamp) - TimeSpan.FromMilliseconds(t.First().Timestamp)).TotalSeconds >= 1)
                // Press and hold F key for 1.5s
                .Subscribe(t =>
                {
                    SendKeyDown(KeyCode.KEY_F);
                    System.Threading.Thread.Sleep(1500);
                    SendKeyUp(KeyCode.KEY_F);
                });
        }
开发者ID:FloatInCodeBlog,项目名称:GamepadAsKeyboard,代码行数:56,代码来源:MainWindow.xaml.cs

示例3: GamePadDevice

        /// <summary>
        /// 番号指定でデバイス生成。番号のデバイスが存在しなければ例外を投げる。
        /// </summary>
        /// <param name="window"></param>
        /// <param name="padNumber">0始まり</param>
        public GamePadDevice(int padNumber, DirectInput directInput)
        {
            var devices = GetDevices(directInput);
            if (devices.Count <= padNumber)
            {
                throw new Exception("指定された数のパッドがつながれていない");
            }
            try
            {
                Stick = new Joystick(directInput, devices[padNumber].InstanceGuid);
                //Stick.SetCooperativeLevel(window, CooperativeLevel.Exclusive | CooperativeLevel.Foreground);
            }
            catch (Exception e)
            {
                throw new Exception("パッドの初期化に失敗", e);
            }

            ///スティックの範囲設定
            foreach (var item in Stick.GetObjects())
            {
                //if ((item.ObjectType & .Axis) != 0)
                //{
                //	Stick.GetObjectPropertiesById((int)item.ObjectType).SetRange(-1000, 1000);
                //}

            }
            Stick.Acquire();
        }
开发者ID:MasakiMuto,项目名称:MasaLibs,代码行数:33,代码来源:GamePadDevice.cs

示例4: Lock_DX_Devices

        //Disable all dinput devices for xinput controllers
        public void Lock_DX_Devices()
        {
            var directInput = new DirectInput();

            try
            {
                IList<DeviceInstance> devicelist = directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly);

                foreach (DeviceInstance cdevice in devicelist)
                {
                    if (cdevice.InstanceName.Trim('\0') == XINPUT_NAME)
                    {
                        var joystick = new Joystick(directInput, cdevice.InstanceGuid);
                        joystick.Acquire();
                        Guid deviceGUID = joystick.Properties.ClassGuid;
                        string devicePath = joystick.Properties.InterfacePath;
                        joystick.Unacquire();
                        string[] dpstlit = devicePath.Split('#');
                        devicePath = @"HID\" + dpstlit[1].ToUpper() + @"\" + dpstlit[2].ToUpper();
                        lockedDevices.Add(new DeviceID(deviceGUID, devicePath));

                        DeviceHelper.SetDeviceEnabled(deviceGUID, devicePath, false);
                    }
                }
            }
            finally
            {
                directInput.Dispose();
            }
        }
开发者ID:TheLastRar,项目名称:SCP2vJoy,代码行数:31,代码来源:DXinputLocker.cs

示例5: Initialize

		public static void Initialize()
		{
			if (dinput == null)
				dinput = new DirectInput();

			Devices = new List<GamePad>();

			foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
			{
				Console.WriteLine("joydevice: {0} `{1}`", device.InstanceGuid, device.ProductName);

				if (device.ProductName.Contains("XBOX 360"))
					continue; // Don't input XBOX 360 controllers into here; we'll process them via XInput (there are limitations in some trigger axes when xbox pads go over xinput)

				var joystick = new Joystick(dinput, device.InstanceGuid);
				joystick.SetCooperativeLevel(GlobalWin.MainForm.Handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);
				foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
				{
					if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
						joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
				}
				joystick.Acquire();

				GamePad p = new GamePad(device.InstanceName, device.InstanceGuid, joystick);
				Devices.Add(p);
			}
		}
开发者ID:henke37,项目名称:BizHawk,代码行数:27,代码来源:GamePad.cs

示例6: DirectInputManager

        public DirectInputManager()
        {
            var directInput = new DirectInput();

            // Prefer a Driving device but make do with fallback to a Joystick if we have to
            var deviceInstance = FindAttachedDevice(directInput, DeviceType.Driving);
            if (null == deviceInstance)
                deviceInstance = FindAttachedDevice(directInput, DeviceType.Joystick);
            if (null == deviceInstance)
                throw new Exception("No Driving or Joystick devices attached.");
            joystickType = (DeviceType.Driving == deviceInstance.Type ? JoystickTypes.Driving : JoystickTypes.Joystick);

            // A little debug spew is often good for you
            Log.Spew("First Suitable Device Selected \"" + deviceInstance.InstanceName + "\":");
            Log.Spew("\tProductName: " + deviceInstance.ProductName);
            Log.Spew("\tType: " + deviceInstance.Type);
            Log.Spew("\tSubType: " + deviceInstance.Subtype);

            // Data for both Driving and Joystick device types is received via Joystick
            joystick = new Joystick(directInput, deviceInstance.InstanceGuid);
            IntPtr consoleWindowHandle = GetConsoleWindow();
            if (IntPtr.Zero != consoleWindowHandle)
            {
                CooperativeLevel cooperativeLevel = CooperativeLevel.Background | CooperativeLevel.Nonexclusive;
                Log.Spew("Console window cooperative level: " + cooperativeLevel);
                if (!joystick.SetCooperativeLevel(consoleWindowHandle, cooperativeLevel).IsSuccess)
                    throw new Exception("Failed to set cooperative level for DirectInput device in console window.");
            }
            var result = joystick.Acquire();
            if (!result.IsSuccess)
                throw new Exception("Failed to acquire DirectInput device.");

            Log.Spew("Joystick acquired.");
        }
开发者ID:pushtechnology,项目名称:blog-steering-wheel,代码行数:34,代码来源:DirectInputManager.cs

示例7: Reconnect

        public override void Reconnect()
        {
            // search for devices
            foreach (DeviceInstance device in _DInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // create the device
                try
                {
                    _Joy = new Joystick(_DInput, device.InstanceGuid);
                    _Joy.SetCooperativeLevel(wnd_int, CooperativeLevel.Nonexclusive | CooperativeLevel.Background);
                    break;
                }
                catch (DirectInputException)
                {
                }
            }
            if (_Joy == null)
            {
                IsConnected = false;
                Name = "";
                Type = "";
                return;
            }

            _Joy.Acquire();

            IsConnected = true;
            Name = _Joy.Information.ProductName;
            Type = "DirectInput";
        }
开发者ID:uamt-brno,项目名称:RaspBot,代码行数:30,代码来源:JoyDirectInput.cs

示例8: GamepadReader

        public GamepadReader()
        {
            Buttons = _buttons;
            Analogs = _analogs;

            _dinput = new DirectInput();

            var devices = _dinput.GetDevices (DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly);
            if (devices.Count < 1) {
                throw new IOException ("GamepadReader could not find a connected gamepad.");
            }
            _joystick = new Joystick (_dinput, devices[0].InstanceGuid);

            foreach (var obj in _joystick.GetObjects()) {
                if ((obj.ObjectType & ObjectDeviceType.Axis) != 0) {
                    _joystick.GetObjectPropertiesById ((int)obj.ObjectType).SetRange (-RANGE, RANGE);
                }
            }

            if (_joystick.Acquire().IsFailure) {
                throw new IOException ("Connected gamepad could not be acquired.");
            }

            _timer = new DispatcherTimer ();
            _timer.Interval = TimeSpan.FromMilliseconds (TIMER_MS);
            _timer.Tick += tick;
            _timer.Start ();
        }
开发者ID:Karl-Parris,项目名称:NintendoSpy,代码行数:28,代码来源:GamepadReader.cs

示例9: MainController

        public MainController()
        {
            var directInput = new DirectInput();
            LogicState = new LogicState();

            foreach (var deviceInstance in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    gamepad = new Joystick(directInput, deviceInstance.InstanceGuid);
                    gamepad.SetCooperativeLevel(Parent, CooperativeLevel.Exclusive | CooperativeLevel.Foreground);
                    break;
                }
                catch (DirectInputException) { }
            }

            if (gamepad == null)
                return;

            foreach (var deviceObject in gamepad.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    gamepad.GetObjectPropertiesById((int) deviceObject.ObjectType).SetRange(-1000, 1000);
            }

            gamepad.Acquire();
        }
开发者ID:martikaljuve,项目名称:Robin,代码行数:27,代码来源:MainController.cs

示例10: SimpleJoystick

        /// 
        /// Construct, attach the joystick
        /// 
        public SimpleJoystick()
        {
            DirectInput dinput = new DirectInput();

            // Search for device
            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Create device
                try
                {
                    Joystick = new Joystick(dinput, device.InstanceGuid);
                    break;
                }
                catch (DirectInputException)
                {
                }
            }

            if (Joystick == null)
                throw new Exception("No joystick found");

            foreach (DeviceObjectInstance deviceObject in Joystick.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    Joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
            }

            // Acquire sdevice
            Joystick.Acquire();
        }
开发者ID:wariw,项目名称:360Joypad_test,代码行数:33,代码来源:SimpleJoystick.cs

示例11: GetJoystick

        private Joystick GetJoystick()
        {
            var directInput = new DirectInput();
            var form = new Form();

            foreach (var device in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                var controller = new Joystick(directInput, device.InstanceGuid);
                controller.SetCooperativeLevel(form.Handle, CooperativeLevel.Exclusive | CooperativeLevel.Background);

                var retries = 0;
                while (controller.Acquire().IsFailure)
                {
                    retries++;
                    if (retries > 500)
                        throw new Exception("Couldnt acquire SlimDX stick");
                }

                if (controller.Information.InstanceName.Contains("PPJoy"))
                {

                    foreach (DeviceObjectInstance deviceObject in controller.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            controller.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }

                    return controller;
                }
            }

            return null;
        }
开发者ID:Cyborg11,项目名称:FreePIE,代码行数:33,代码来源:PPJoyPluginTest.cs

示例12: InitializeController

 public void InitializeController(Guid initGuid)
 {
     controllerGuid = Guid.Empty;
     var deviceInst = input.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly);
     if (deviceInst.Count == 0)
     {
         deviceInst = input.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly);
     }
     if (deviceInst.Count > 0)
     {
         foreach (var device in deviceInst)
         {
             if (device.InstanceGuid == initGuid)
             {
                 controllerGuid = initGuid;
             }
         }
         if (controllerGuid == Guid.Empty)
         {
             controllerGuid = deviceInst[0].InstanceGuid;
         }
         controller = new Joystick(input, controllerGuid);
         controller.Acquire();
         defaultControllerState = controller.GetCurrentState();
     }
 }
开发者ID:Silenthal,项目名称:gbemu,代码行数:26,代码来源:Win32InputHandler.cs

示例13: btnOpenPad_Click

        private void btnOpenPad_Click(object sender, EventArgs e)
        {
            if (jp != null)
                jp.Dispose();

            jp = new Joystick(di, (cbPad.SelectedItem as xJoypad).dix.InstanceGuid);
            jp.Acquire();

            lblPadStatus.Text = "Open: " + jp.Information.InstanceName;
        }
开发者ID:skmp,项目名称:quadrotor-hacks,代码行数:10,代码来源:Form1.cs

示例14: GamePad

        public GamePad()
        {
            // Initialize DirectInput
            directInput = new DirectInput();

            // Find a Joystick Guid
            joystickGuid = Guid.Empty;

            // Find a Gamepad
            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                        DeviceEnumerationFlags.AllDevices))
                joystickGuid = deviceInstance.InstanceGuid;

            // If Gamepad not found, look for a Joystick
            if (joystickGuid == Guid.Empty)
                foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
                        DeviceEnumerationFlags.AllDevices))
                    joystickGuid = deviceInstance.InstanceGuid;

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Logger.Log(this, "No joystick/Gamepad found.", 2);            
            }
            else
            {
                // Instantiate the joystick
                joystick = new Joystick(directInput, joystickGuid);

                Logger.Log(this, String.Format("Found Joystick/Gamepad with GUID: {0}", joystickGuid), 1);

                // Set BufferSize in order to use buffered data.
                joystick.Properties.BufferSize = 128;

                // Acquire the joystick
                joystick.Acquire();

                // Poll events from joystick


                //while (true)
                //{
                //    joystick.Poll();
                //    var datas = joystick.GetBufferedData();
                //    foreach (var state in datas)
                //        Console.WriteLine(state);
                //}

                timer = new Timer();
                timer.Interval = TIMER_INTERVAL_IN_MS;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }
        }
开发者ID:rAum,项目名称:auton_net,代码行数:54,代码来源:GamePad.cs

示例15: UseDevices

        private void UseDevices(IList<DeviceInstance> devices)
        {
            var guid = devices[0].InstanceGuid; // use first one
            _joyStick = new Joystick(directInput, guid);

            _joyStick.Properties.BufferSize = 128; // enable buffer
            _joyStick.Acquire();

            var timer = new DispatcherTimer(); // Timer(onTimer, null, 100);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();
        }
开发者ID:john-jay,项目名称:DirectInputDemo,代码行数:13,代码来源:MainWindow.xaml.cs


注:本文中的Joystick.Acquire方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。