none
Simulating Extended Key Press Based on Received UDP Packets

    问题

  • Hi,

        I'm working on a simple C# program that listens for UDP packets and translates the received data to keystrokes or mouse events.  I am streaming the output of a MATLAB/Simulink model to my C# program via UDP, and I use a switch statement to determine which keystrokes to to simulate.  The UDP stream comes from a real-time pattern recognition model that is constantly running and analyzing biological signals.

        I would like to simulate an extended key press.  Specifically, I would like to press and hold 'ctrl' or some other key for as long as 'ctrl' is the desired output signal.  Currently my program would do something like:

    nothing...nothing...nothing...ctrl...ctrl...ctrl...nothing...nothing...nothing - where each of these keystrokes is 25ms apart.  I would like for this program to do something like:

    nothing...nothing...nothing...ctrl (holding down until the signal changes...)..................nothing...nothing...nothing

         I'm using SendKeys.SendWait(keystroke) to output the desired keystrokes to a particular window, but I have only come across examples of how to send multiple keystrokes, not extended keystrokes.  It would be simple enough for me to use my existing code if I could say SendKeys.SendWait(ctrl down) the first time 'ctrl' is requested and then SendKeys.SendWait(ctrl up) when the desired action changes.

         Why would I want to do this?  As an example, I want to simulate 'ctrl' being pressed while the user points and clicks with the mouse.  This is required in some graphics programs to resize objects while maintaining dimensional relationships.  If you have only one hand, this is quite difficult.   If I can get the 'ctrl' signal from elsewhere, problem solved.

         Any tips are much appreciated.  Thanks in advance.

    -MP

    • 已移动 Helen Zhao 2012年3月14日 4:16 (From:Visual C++ Language)
    2012年3月13日 1:27

答案

  • Hi M Powell,

    Welcome to the MSDN forum.

    If you want to simulate an extended key press, you can try keybd_event. The below sample is about holding down Escape key for 5 seconds and then release it.

    [DllImport("user32.dll")] 
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); 
     
     
    keybd_event(VK_ESCAPE, 0, 0, 0) // KEY_DOWN 
     
    System.Threading.Thread.Sleep(5000); 
     
    keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0) // KEY_UP 
    

    The links below may help you too.

    Keyboard Events Simulation using keybd_event() function

    http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd_event-funct

    Windows Input Simulator (C# SendInput Wrapper - Simulate Keyboard and Mouse)

    http://inputsimulator.codeplex.com/

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 M Powell 2012年3月14日 17:09
    2012年3月14日 8:36

全部回复

  • Hi M Powell,

    According to your description, I'd like to move this thread to "Visual C# General Forum" for better support, where more experts live. We only disscuss issues regarding the C++ language, compiler and linker in Visual C++ Language Forum.

    Thanks for your understanding.
    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    2012年3月14日 4:16
  • Hi M Powell,

    Welcome to the MSDN forum.

    If you want to simulate an extended key press, you can try keybd_event. The below sample is about holding down Escape key for 5 seconds and then release it.

    [DllImport("user32.dll")] 
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo); 
     
     
    keybd_event(VK_ESCAPE, 0, 0, 0) // KEY_DOWN 
     
    System.Threading.Thread.Sleep(5000); 
     
    keybd_event(VK_ESCAPE, 0, KEYEVENTF_KEYUP, 0) // KEY_UP 
    

    The links below may help you too.

    Keyboard Events Simulation using keybd_event() function

    http://www.codeproject.com/Articles/7305/Keyboard-Events-Simulation-using-keybd_event-funct

    Windows Input Simulator (C# SendInput Wrapper - Simulate Keyboard and Mouse)

    http://inputsimulator.codeplex.com/

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    • 已标记为答案 M Powell 2012年3月14日 17:09
    2012年3月14日 8:36
  • Thanks, Bob.  Since my post I have had success using the SendKeys, keybd_event, and InputSimulator when trying to send keys to several programs, but some programs (games in particular) don't respond to the keybd_event as if it was an actual keystroke on the keyboard.  The keybd_event allows me to specify the key_down and key_up just like I needed.  Now I need to truly simulate a real keystroke in a way that a game can recognize.  Thanks again for the help.
    2012年3月14日 17:09