how can i capture mupltiple keypresses on C# windows application?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • andkar
    New Member
    • Oct 2008
    • 2

    how can i capture mupltiple keypresses on C# windows application?

    hi all
    im trying to capture multiple key passes like up arrow + left arrow
    is there any way to do that with a key event or i have to write my own code
    thank you
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well they will throw seperate keydown events, that could be helpfull? (Actually I think they continuously to throw keydown events)

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      Well, I'm thinking that you'd have to use the KeyEventArgs class and some code to catch multiple events, like whenever the key is down set a boolean to true, and when the key is up set the boolean to false? And each time the key is pressed, after setting the booleans' value(s), have an if statement to see if two are down at the same time, although there are simpler ways.

      joedeene

      Comment

      • mansuri
        New Member
        • Oct 2008
        • 1

        #4
        You have to do some keyboard hooking to get all events. I am pasting some code from my app where I am doing it. Its not working code but you will have idea

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboar dProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;


        [DllImport("user 32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookE x(int idHook,
        LowLevelKeyboar dProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user 32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(Unman agedType.Bool)]
        private static extern bool UnhookWindowsHo okEx(IntPtr hhk);

        [DllImport("user 32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx( IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

        [DllImport("kern el32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle (string lpModuleName);

        public static void Start()
        {
        _hookID = SetHook(_proc);

        }
        public static void Stop()
        {
        UnhookWindowsHo okEx(_hookID);
        }


        private static IntPtr SetHook(LowLeve lKeyboardProc proc)
        {
        using (Process curProcess = Process.GetCurr entProcess())
        using (ProcessModule curModule = curProcess.Main Module)
        {
        return SetWindowsHookE x(WH_KEYBOARD_L L, proc,
        GetModuleHandle (curModule.Modu leName), 0);
        }
        }

        private delegate IntPtr LowLevelKeyboar dProc(
        int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
        {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYD OWN)
        {
        int vkCode = Marshal.ReadInt 32(lParam);
        string keycode = (Keys)vkCode; }
        return CallNextHookEx( _hookID, nCode, wParam, lParam);
        }

        Comment

        • andkar
          New Member
          • Oct 2008
          • 2

          #5
          Originally posted by joedeene
          Well, I'm thinking that you'd have to use the KeyEventArgs class and some code to catch multiple events, like whenever the key is down set a boolean to true, and when the key is up set the boolean to false? And each time the key is pressed, after setting the booleans' value(s), have an if statement to see if two are down at the same time, although there are simpler ways.

          joedeene
          what kind of simpler ways???

          Comment

          • IanWright
            New Member
            • Jan 2008
            • 179

            #6
            Originally posted by andkar
            what kind of simpler ways???
            That is a simple way....

            public void KeyDown(object sender, KeyEventArgs e)
            {
            if(e.KeyCode == Keys.Left)
            {
            // do something
            }
            else if (e.KeyCode == Keys.Right)
            {
            // do something else
            }
            }

            Comment

            Working...