Click here to Skip to main content
15,038,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store keyboard input in an array until keyboard passes # character and this array got # it will pass control to next code. I got this code from RowInput project of codeProject. it works fine and passes when at least one key pressed. but i want to store till # comes , is it possible please give me a way?

here is my code:

C#
protected override void WndProc(ref Message message)
      {
          switch (message.Msg)
          {
              case Win32.WM_INPUT:
                  {
                      _keyboardDriver.ProcessRawInput(message.LParam);
                  }
                  break;

              case Win32.WM_USB_DEVICECHANGE:
                  {
                      Debug.WriteLine("USB Device Arrival / Removal");
                      _keyboardDriver.EnumerateDevices();
                  }
                  break;
          }

          base.WndProc(ref message);
      }


  public void ProcessRawInput(IntPtr hdevice)
      {
          //Debug.WriteLine(_rawBuffer.data.keyboard.ToString());
          //Debug.WriteLine(_rawBuffer.data.hid.ToString());
          //Debug.WriteLine(_rawBuffer.header.ToString());

          if (_deviceList.Count == 0) return;

          var dwSize = 0;
          Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, IntPtr.Zero, ref dwSize, Marshal.SizeOf(typeof(Rawinputheader)));

          if (dwSize != Win32.GetRawInputData(hdevice, DataCommand.RID_INPUT, out _rawBuffer, ref dwSize, Marshal.SizeOf(typeof(Rawinputheader))))
          {
              Debug.WriteLine("Error getting the rawinput buffer");
              return;
          }

          int virtualKey = _rawBuffer.data.keyboard.VKey;

          int makeCode = _rawBuffer.data.keyboard.Makecode;
          int flags = _rawBuffer.data.keyboard.Flags;

          if (virtualKey == Win32.KEYBOARD_OVERRUN_MAKE_CODE) return;

          var isE0BitSet = ((flags & Win32.RI_KEY_E0) != 0);

          KeyPressEvent keyPressEvent;

          if (_deviceList.ContainsKey(_rawBuffer.header.hDevice))
          {
              lock (_padLock)
              {
                  keyPressEvent = _deviceList[_rawBuffer.header.hDevice];
              }
          }
          else
          {
              Debug.WriteLine("Handle: {0} was not in the device list.", _rawBuffer.header.hDevice);
              return;
          }

          var isBreakBitSet = ((flags & Win32.RI_KEY_BREAK) != 0);

          keyPressEvent.KeyPressState = isBreakBitSet ? "BREAK" : "MAKE";
          keyPressEvent.Message = _rawBuffer.data.keyboard.Message;
          keyPressEvent.VKeyName = KeyMapper.GetKeyName(VirtualKeyCorrection(virtualKey, isE0BitSet, makeCode)).ToUpper();
          keyPressEvent.VKey = virtualKey;

          if (KeyPressed != null)
          {
              KeyPressed(this, new RawInputEventArg(keyPressEvent));
          }
      }
Posted
Comments
[no name] 12-Nov-15 1:17am
   
What have you tried? To simply get keyboard input in a windows form this is not the way. Google it. e.g. http://www.geekpedia.com/tutorial53_Getting-input-from-keyboard.html
Sinisa Hajnal 12-Nov-15 2:51am
   
Is this winform? Console app? What? Please explain what you need and in which setup. Thank you.
Member 11543226 12-Nov-15 5:20am
   
winform application
Sinisa Hajnal 13-Nov-15 6:27am
   
In winform, you don't have to hook into message pump. Enable KeyPreview property of the form and handle keydown adding (and deleting in case of Delete and Backspace buttons) into your array until you get your end character.

Good luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900