R
ReeceAlqotaibi
Guest
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.
Continue reading...