public override void Update(TimeInfo timeInfo) { // Be sure that the device is still acquired if (keyboard.Acquire().IsFailure) { return; } // Poll for more input if (keyboard.Poll().IsFailure) { return; } // Get the list of buffered data events BufferedDataCollection <KeyboardState> bufferedData = keyboard.GetBufferedData(); if (Result.Last.IsFailure) { return; } // Update key states and raise appropriate events foreach (BufferedData <KeyboardState> packet in bufferedData) { foreach (Key key in packet.Data.PressedKeys) { if (!keyState[key]) { keyState[key] = true; keyEventArgs.Key = key; if (KeyDown != null) { KeyDown(this, keyEventArgs); } } } foreach (Key key in packet.Data.ReleasedKeys) { if (keyState[key]) { keyState[key] = false; keyEventArgs.Key = key; if (KeyUp != null) { KeyUp(this, keyEventArgs); } } } } }
void ProcessInput() { //----------------------------------------------------------------------------- // Name: ProcessInput() // Desc: Gathers user input, plays audio, and draws output. Input is gathered // from the DInput devices, and output is displayed in the app's window. //----------------------------------------------------------------------------- // Loop through all devices and check game input applicationDevices.Reset(); foreach (SemanticsInstance instance in applicationDevices) { BufferedDataCollection data = null; // Need to ensure that the devices are acquired, and pollable devices // are polled. try { instance.Device.Acquire(); instance.Device.Poll(); data = instance.Device.GetBufferedData(); } catch (InputException) { continue; } if (null == data) { continue; } // Get the sematics codes. // Each event has a type stored in "ApplicationData", and actual data is stored in // "Data". foreach (BufferedData d in data) { // Non-axis data is recieved as "button pressed" or "button // released". Parse input as such. bool state = (0x80 == d.Data) ? true : false; int index = (int)d.ApplicationData; if (buttonStates[index] == false && state) { PlaySound(index); boxColors[index] = 255.0f; } buttonStates[index] = state; } } DrawRects(false); }
private void btnRead_Click(object sender, System.EventArgs e) { string result = String.Empty; // получаем буфер данных dataCollection = device.GetBufferedData(); // если есть данные - отображаем if (dataCollection != null) { foreach (BufferedData d in dataCollection) { // выводим смещение и код result += String.Format(" 0x{0:X2}=0x{1:X2} ", d.Offset, d.Data); } } tbResult.Text = result; }
private async void BeginPoll() { try { if (!this.IsValid) { return; } this.IsPolling = true; Label_007E: if (this.cts.IsCancellationRequested) { return; } this._joystick.Poll(); BufferedDataCollection bufferedData = this._joystick.GetBufferedData(); if (bufferedData != null) { IEnumerator enumerator = bufferedData.GetEnumerator(); try { while (enumerator.MoveNext()) { BufferedData current = (BufferedData)enumerator.Current; if (current.ButtonPressedData == 1) { int button = Array.FindIndex <byte>(this.Buttons, b => b != 0); if (button == -1) { continue; } this.OnButtonPressed(button); goto Label_03F5; } if (current.ButtonPressedData == 0) { if (this.LeftStickX > 0xafc8) { this.OnDirectionChanged("right"); await Task.Delay(250); } else if (this.LeftStickX < 0x4a38) { this.OnDirectionChanged("left"); await Task.Delay(250); } if (this.LeftStickY > 0xafc8) { this.OnDirectionChanged("down"); await Task.Delay(250); continue; } if (this.LeftStickY < 0x4a38) { this.OnDirectionChanged("up"); await Task.Delay(250); } } } } finally { IDisposable asyncVariable1; asyncVariable1 = enumerator as IDisposable; if (asyncVariable1 != null) { asyncVariable1.Dispose(); } } } Label_03F5: if (this._joystick != null) { switch (this.DirectionalButtons[0]) { case 0: this.OnDirectionChanged("up"); await Task.Delay(250); break; case 0x2328: this.OnDirectionChanged("right"); await Task.Delay(250); break; case 0x4650: this.OnDirectionChanged("down"); await Task.Delay(250); break; case 0x6978: this.OnDirectionChanged("left"); await Task.Delay(250); break; } } goto Label_007E; } catch (Exception exception) { this.IsPolling = false; Console.WriteLine(exception.Message); } }
///<summary> /// Read the input device's state when in buffered mode and display it. ///</summary> bool ReadBufferedData() { BufferedDataCollection dataCollection = null; string textNew = String.Empty; if (null == applicationDevice) { return(true); } InputException ie = null; try { dataCollection = applicationDevice.GetBufferedData(); } catch (InputException) { // We got an error // // It means that continuous contact with the // device has been lost, either due to an external // interruption, or because the buffer overflowed // and some events were lost. // // Consequently, if a button was pressed at the time // the buffer overflowed or the connection was broken, // the corresponding "up" message might have been lost. // // But since our simple sample doesn't actually have // any state associated with button up or down events, // there is no state to reset. (In a real game, ignoring // the buffer overflow would result in the game thinking // a key was held down when in fact it isn't; it's just // that the "up" event got lost because the buffer // overflowed.) // // If we want to be more clever, we could do a // GetBufferedData() and compare the current state // against the state we think the device is in, // and process all the states that are currently // different from our private state. bool loop = true; do { try { applicationDevice.Acquire(); } catch (InputLostException) { loop = true; } catch (InputException inputException) { ie = inputException; loop = false; } }while (loop); // Update the dialog text. if (ie is OtherApplicationHasPriorityException || ie is NotAcquiredException) { dataLabel.Text = "Unacquired"; } // This // may occur when the app is minimized or in the process of // switching, so just try again later. return(true); } if (null != ie) { return(false); } if (null == dataCollection) { return(true); } // Study each of the buffer elements and process them. // // Since we really don't do anything, our "processing" // consists merely of squirting the name into our // local buffer. foreach (BufferedData d in dataCollection) { // this will display then scan code of the key // plus a 'D' - meaning the key was pressed // or a 'U' - meaning the key was released textNew += String.Format("0x{0:X}", d.Offset); textNew += (0 != (d.Data & 0x80)) ? "D " : "U "; } // If nothing changed then don't repaint - avoid flicker. if (dataLabel.Text != textNew) { dataLabel.Text = textNew; } return(true); }
private void DataArrivalHandler() { while (Created) { DataArrivalEvent.WaitOne(); if (!Created) { break; } BufferedDataCollection col = null; try { col = ApplicationDevice.GetBufferedData(); } catch (InputException e) { if (e is InputLostException || e is AcquiredException) { SetAcquire(true); continue; } } if (null == col) { continue; } foreach (BufferedData data in col) { switch (data.Offset) { case (int)MouseOffset.X: UpdateCursorPosition(data.Data, 0); break; case (int)MouseOffset.Y: UpdateCursorPosition(0, data.Data); break; case (int)MouseOffset.Button0: if (0 != (data.Data & 0x80)) { Cursor = CursorBlank; OldPoint = CurrentPoint = PointToClient(Cursor.Position); Drawing = true; } else { Cursor = Cursors.Cross; Cursor.Position = PointToScreen(CurrentPoint); Drawing = false; } break; case (int)MouseOffset.Button1: SetAcquire(false); ApplicationMenu.Show(this, new Point(10, 10)); break; } } if (Drawing) { ApplicationGraphics.DrawLine(new Pen(Color.Red), new Point(CurrentPoint.X, CurrentPoint.Y), OldPoint); OldPoint = CurrentPoint; } } }
private void ReadBufferedData() { BufferedDataCollection buffData = new BufferedDataCollection(); try { keyboard.Poll(); buffData = keyboard.GetBufferedData(); } catch (NotBufferedException ex) { Logging.Logger.AddWarning("Chyba pri vyberu bufferovanych dat " + ex.ToString()); return; } catch (InputException ex) { Logging.Logger.AddWarning("CHYBA V INPUTU!\n" + ex.ToString()); try { FreeDirectInput(); InitInputDevices(); buffData = keyboard.GetBufferedData(); } catch (InputException iex) { Logging.Logger.AddError("CHYBA V INPUTU!\n" + iex.ToString()); return; } } if (buffData == null || buffData.Count == 0) { return; } foreach (BufferedData data in buffData) { if ((data.Data & 0x80) != 0) //pressed keys { switch (data.Offset) { case (int)DXKey.W: this.bufferedActions.Add(Action.MoveForwardStart); break; case (int)DXKey.S: this.bufferedActions.Add(Action.MoveBackwardStart); break; case (int)DXKey.A: this.bufferedActions.Add(Action.MoveLeftStart); break; case (int)DXKey.D: this.bufferedActions.Add(Action.MoveRightStart); break; } } else //released keys { switch (data.Offset) { case 17: this.bufferedActions.Add(Action.MoveForwardStop); break; case 31: this.bufferedActions.Add(Action.MoveBackwardStop); break; case 30: this.bufferedActions.Add(Action.MoveLeftStop); break; case 32: this.bufferedActions.Add(Action.MoveRightStop); break; case 1: this.bufferedActions.Add(Action.Exit); break; case 0x16: this.bufferedActions.Add(Action.InvertMouse); break; case 0x29: this.bufferedActions.Add(Action.ShowConsole); break; case 0x1C: this.bufferedActions.Add(Action.ChangeFillMode); break; case 0x21: this.bufferedActions.Add(Action.Action3); break; } } } }