Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

RawInput invaild operation error when closing form #50

Open
gomidas opened this issue Sep 12, 2017 · 0 comments
Open

RawInput invaild operation error when closing form #50

gomidas opened this issue Sep 12, 2017 · 0 comments

Comments

@gomidas
Copy link

gomidas commented Sep 12, 2017

I get this error when I close app. what am I doing wrong ?

InputHandler.cs

using SharpDX.Multimedia;
using SharpDX.RawInput;
using System;
using System.Threading;
/*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;*/
using System.Windows.Forms;

namespace CubeApp
{
    public class InputHandler
    {
        public bool KeyRight = false;
        public bool KeyLeft  = false;
        public bool KeyDown  = false;
        public bool KeyUp    = false;

        //COMMON WASD (FPShooter) KEYS
        public bool KeyW = false;
        public bool KeyA = false;
        public bool KeyS = false;
        public bool KeyD = false;

        //COMMON ROTATION KEYS (E,Q)
        public bool KeyE = false;
        public bool KeyQ = false;

        //MOUSE BUTTONS
        public bool MouseLeftButton = false;
        public bool MouseRightButton = false;
        public bool MouseMiddleButton = false;


        //CAMERA ROTATION
        public bool KeyR = false;
        public bool KeyT = false;
        public bool KeyY = false;
        public bool KeyU = false;

        public InputHandler(RenderForm_EX form)
        {
            form.inputHandler = this;

            // setup the device
            if (form.IsHandleCreated) {
            Device.RegisterDevice(UsagePage.Generic, UsageId.GenericMouse, DeviceFlags.None);
            Device.MouseInput += (sender, args) => form.Invoke(new UpdateTextCallback(UpdateMouseText), args);
            Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, DeviceFlags.None);
            Device.KeyboardInput += (sender, args) => form.Invoke(new UpdateTextCallback(UpdateKeyboardText), args);

            }
        }

        void UpdateKeyboardText(RawInputEventArgs rawArgs)
        {
            var args = (KeyboardInputEventArgs)rawArgs;

            if (args.Key == Keys.Right && args.State == KeyState.KeyDown ) { KeyRight =  true; Console.WriteLine(Keys.Right+"[Down]");     }
            if (args.Key == Keys.Right && args.State == KeyState.KeyUp   ) { KeyRight =  false; Console.WriteLine(Keys.Right + "[Up]"); }
            if (args.Key == Keys.Left && args.State  == KeyState.KeyDown ) { KeyLeft  =  true; Console.WriteLine(Keys.Right + "[Down]"); }
            if (args.Key == Keys.Left && args.State  == KeyState.KeyUp   ) { KeyLeft  =  false; Console.WriteLine(Keys.Right + "[Up]"); }
            if (args.Key == Keys.Down && args.State  == KeyState.KeyDown ) { KeyDown  =  true; Console.WriteLine(Keys.Right + "[Down]"); }
            if (args.Key == Keys.Down && args.State  == KeyState.KeyUp   ) { KeyDown  =  false; Console.WriteLine(Keys.Right + "[Up]"); }
            if (args.Key == Keys.Up && args.State    == KeyState.KeyDown ) { KeyUp    =  true; Console.WriteLine(Keys.Right + "[Down]"); }
            if (args.Key == Keys.Up && args.State    == KeyState.KeyUp   ) { KeyUp    =  false; Console.WriteLine(Keys.Right + "[Up]"); }

            //COMMON WASD KEYS
            if (args.Key == Keys.W && args.State == KeyState.KeyDown) { KeyW= true; Console.WriteLine(Keys.W + "[Down]"); }
            if (args.Key == Keys.W && args.State == KeyState.KeyUp) { KeyW = false; Console.WriteLine(Keys.W + "[Up]"); }
            if (args.Key == Keys.A && args.State == KeyState.KeyDown) { KeyA = true; Console.WriteLine(Keys.A + "[Down]"); }
            if (args.Key == Keys.A && args.State == KeyState.KeyUp) { KeyA = false; Console.WriteLine(Keys.A + "[Up]"); }
            if (args.Key == Keys.S && args.State == KeyState.KeyDown) { KeyS = true; Console.WriteLine(Keys.S + "[Down]"); }
            if (args.Key == Keys.S && args.State == KeyState.KeyUp) { KeyS = false; Console.WriteLine(Keys.S + "[Up]"); }
            if (args.Key == Keys.D && args.State == KeyState.KeyDown) { KeyD = true; Console.WriteLine(Keys.D + "[Down]"); }
            if (args.Key == Keys.D && args.State == KeyState.KeyUp) { KeyD = false; Console.WriteLine(Keys.D + "[Up]"); }

            //COMMON E,Q KEYS
            if (args.Key == Keys.E && args.State == KeyState.KeyDown) { KeyE = true; Console.WriteLine(Keys.E + "[Down]"); }
            if (args.Key == Keys.E && args.State == KeyState.KeyUp) { KeyE = false; Console.WriteLine(Keys.E + "[Up]"); }
            if (args.Key == Keys.Q && args.State == KeyState.KeyDown) { KeyQ = true; Console.WriteLine(Keys.Q + "[Down]"); }
            if (args.Key == Keys.Q && args.State == KeyState.KeyUp) { KeyQ = false; Console.WriteLine(Keys.Q + "[Up]"); }

            //ROTATE CAMERA R,T
            if (args.Key == Keys.R && args.State == KeyState.KeyDown) { KeyR = true; Console.WriteLine(Keys.R + "[Down]"); }
            if (args.Key == Keys.R && args.State == KeyState.KeyUp) { KeyR = false; Console.WriteLine(Keys.R + "[Up]"); }
            if (args.Key == Keys.T && args.State == KeyState.KeyDown) { KeyT = true; Console.WriteLine(Keys.T + "[Down]"); }
            if (args.Key == Keys.T && args.State == KeyState.KeyUp) { KeyT = false; Console.WriteLine(Keys.T + "[Up]"); }

            //ROTATE CAMERA Y,U
            if (args.Key == Keys.Y && args.State == KeyState.KeyDown) { KeyY = true; Console.WriteLine(Keys.Y + "[Down]"); }
            if (args.Key == Keys.Y && args.State == KeyState.KeyUp) { KeyY = false; Console.WriteLine(Keys.Y + "[Up]"); }
            if (args.Key == Keys.U && args.State == KeyState.KeyDown) { KeyU = true; Console.WriteLine(Keys.U + "[Down]"); }
            if (args.Key == Keys.U && args.State == KeyState.KeyUp) { KeyU = false; Console.WriteLine(Keys.U + "[Up]"); }
        }
        /// <summary>
        /// Updates the mouse text.
        /// </summary>
        /// <param name="rawArgs">The <see cref="SharpDX.RawInput.RawInputEventArgs"/> instance containing the event data.</param>
        void UpdateMouseText(RawInputEventArgs rawArgs)
        {
            var args = (MouseInputEventArgs)rawArgs;
            //LEFT BUTTON
            if (args.ButtonFlags == MouseButtonFlags.Button1Down) { MouseLeftButton = true; Console.WriteLine(MouseButtons.Left + "[Down]"); }
            if (args.ButtonFlags == MouseButtonFlags.Button1Up) { MouseLeftButton = false; Console.WriteLine(MouseButtons.Left + "[Up]"); }
            //RIGHT BUTTON
            if (args.ButtonFlags == MouseButtonFlags.Button2Down) { MouseRightButton = true; Console.WriteLine(MouseButtons.Right + "[Down]"); }
            if (args.ButtonFlags == MouseButtonFlags.Button2Up) { MouseRightButton = false; Console.WriteLine(MouseButtons.Right + "[Up]"); }
            //MIDDLE BUTTON
            if (args.ButtonFlags == MouseButtonFlags.Button3Down) { MouseRightButton = true; Console.WriteLine(MouseButtons.Middle + "[Down]"); }
            if (args.ButtonFlags == MouseButtonFlags.Button3Up) { MouseRightButton = false; Console.WriteLine(MouseButtons.Middle + "[Up]"); }
        }
        /// <summary>
        /// Delegate use for printing events
        /// </summary>
        /// <param name="args">The <see cref="SharpDX.RawInput.RawInputEventArgs"/> instance containing the event data.</param>
        public delegate void UpdateTextCallback(RawInputEventArgs args);
    }
}

I call it like : InputHandler IHandler = new InputHandler(form);

System.InvalidOperationException oluştu
  HResult=0x80131509
  İleti=Pencere işleyicisi oluşturuluncaya kadar denetim üzerinde Invoke veya BeginInvoke çağrılamaz.
  Kaynak=System.Windows.Forms
  StackTrace:
   konum System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
   konum System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
   konum CubeApp.InputHandler.<>c__DisplayClass17_0.<.ctor>b__0(Object sender, MouseInputEventArgs args) C:\Users\gomid_000\Documents\Visual Studio 2017\Projects\CubeApp\CubeApp\InputHandler.cs içinde: 50. satır
   konum SharpDX.RawInput.Device.HandleMessage(IntPtr rawInputMessagePointer, IntPtr hwnd)
   konum SharpDX.RawInput.Device.RawInputMessageFilter.PreFilterMessage(Message& m)
   konum System.Windows.Forms.Application.ThreadContext.ProcessFilters(MSG& msg, Boolean& modified)
   konum System.Windows.Forms.Application.FilterMessage(Message& message)
   konum SharpDX.Windows.RenderLoop.NextFrame()
   konum SharpDX.Windows.RenderLoop.Run(Control form, RenderCallback renderCallback, Boolean useApplicationDoEvents)
   konum CubeApp.Program.<>c__DisplayClass4_0.<Main>b__4() C:\Users\gomid_000\Documents\Visual Studio 2017\Projects\CubeApp\CubeApp\Program.cs içinde: 310. satır
   konum System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   konum System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   konum System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   konum System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   konum System.Threading.ThreadHelper.ThreadStart()
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant