none
Simulating Key Presses RRS feed

  • Question

  • Intro:

    I'm not really great at programming, just something I'm trying to learn in my spare time and when the opportunity arises for little projects at work.

    I'm trying to create a basic application which simulates the key presses for (Ctrl + Win + Shift) + B, with the keys in parenthesis being held until B is pressed.

    The reason for this being that some of our users are experiencing issue with their monitors, and resetting the display driver seems to do the trick.

    The code:

    Method One:

    using System.Windows.Forms;
    using WindowsInput.Native;
    using WindowsInput;


    namespace FixMonitors
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                var simu = new InputSimulator();
                simu.Keyboard.ModifiedKeyStroke(new[] { VirtualKeyCode.LWIN, VirtualKeyCode.CONTROL, VirtualKeyCode.SHIFT }, VirtualKeyCode.VK_B);
                this.Close();
            }
        }
    }

    Method Two:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;


    namespace FixMonitors
    {
        static class KeyboardSend
        {
            [DllImport("user32.dll")]
            private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

            private const int KEYEVENTF_EXTENDEDKEY = 1;
            private const int KEYEVENTF_KEYUP = 2;

            public static void KeyDown(Keys vKey)
            {
                keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
            }

            public static void KeyUp(Keys vKey)
            {
                keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
            this.Close();
                KeyboardSend.KeyDown(Keys.LControlKey);
                KeyboardSend.KeyDown(Keys.LWin);
                KeyboardSend.KeyDown(Keys.LShiftKey);
                KeyboardSend.KeyDown(Keys.B);
                KeyboardSend.KeyUp(Keys.B);
                KeyboardSend.KeyUp(Keys.LShiftKey);
                KeyboardSend.KeyUp(Keys.LWin);
                KeyboardSend.KeyUp(Keys.LControlKey);
            }
        }
    }

    The problem:

    Now, when I build either of these in Visual Studio and run the .exe that is created everything works fine on my desktop.    

    I copy the exe and test on four other desktops along with mine and it works fine for three of them; the screens go blank and the display driver resets. On the fourth machine absolutely nothing happens whatsoever and I cannot figure out why, so any help in diagnosing this would be greatly appreciated.

    What I have tried:

    I have tried installing .NET 3.5.1 and .NET 4.8 to the machine in question (as this is what I have installed on mine).

    I have also tried to rebuild the app specifically targeted ad .NET 3.5 and .NET 4.7.2, but to no avail.

    Further to this, I have changed the code slightly whilst trying to diagnose so that only the Win key is pressed (simulating opening the start menu) and this works fine on the machine in question.

    Again, any help would be greatly appreciated.

    Thanks in advance.

    Wednesday, August 7, 2019 7:19 PM

All replies

  • Hi ReeceAlqotaibi,

    Thank you for posting here.

    Based on your description, you failed to Simulating Key when you tested on the fourth machine.

    I am not sure if  it has some difference among four machines. Therefore, I find a new method to do it.

    Code:

      [DllImport("user32.dll")]
            public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo);
    
            private void Button2_Click(object sender, EventArgs e)
            {
                keybd_event(0x5b, 0, 0, 0);
                SendKeys.Send("^+{B}");
                keybd_event(0x5b, 0, 0x2, 0);
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 8:15 AM
    Moderator
  • My guess is that your fourth system has a different graphics adapter, perhaps a cheaper one that does not support the "reset" function.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, August 9, 2019 5:51 AM
  • Hi Tim,

    The drivers are the same across the board and all the machines are identical.

    Further to this, the driver does support the reset function as I can press these keys manually and it works without a problem.

    Friday, August 9, 2019 9:12 AM
  • Hi Jack,

    Thanks for your reply. However this produces exactly the same results as before. There are no differences with the machines.

    Best regards,
    Reece

    Tuesday, August 13, 2019 8:43 AM
  • Hi ReeceAlqotaibi,

    Thanks for the feedback.

    >>The drivers are the same across the board and all the machines are identical.

    I think the code may be not a problem. Therefore, I suggest that you could post it in the machines related forum.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 14, 2019 7:18 AM
    Moderator
  • The drivers are the same across the board and all the machines are identical.

    Further to this, the driver does support the reset function as I can press these keys manually and it works without a problem.

    I could reproduce the problem on my PC, with Intel(R) HD Graphics

    It works manually but not by simulating keys.

    I tested in C# and in C++, with SendInput

    (I can simulate any combination of keys (as Admin for some games), but not [Ctrl] + [Win] + [Shift] + "B"...)

    Wednesday, August 14, 2019 7:35 AM