3

Try to use InputSimulator to simulate keyboard inputs. Everything works fine except using sim.Keyboard.ModifiedKeyStroke to simulate the input of ASCII character.

I tried to simulate Alt down + numpad1 + numpad4 + numpad7 + Alt up using the following two different ways:

    sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, new[] { VirtualKeyCode.NUMPAD1, VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD7});

and

    sim.Keyboard.KeyDown(VirtualKeyCode.LMENU);
    sim.Keyboard.KeyDown(VirtualKeyCode.NUMPAD1);
    sim.Keyboard.KeyUp(VirtualKeyCode.NUMPAD1);
    sim.Keyboard.KeyDown(VirtualKeyCode.NUMPAD4);
    sim.Keyboard.KeyUp(VirtualKeyCode.NUMPAD4);
    sim.Keyboard.KeyDown(VirtualKeyCode.NUMPAD7);
    sim.Keyboard.KeyUp(VirtualKeyCode.NUMPAD7);
    sim.Keyboard.KeyUp(VirtualKeyCode.LMENU);

Neither works. I try to print out key status in console, the real key press and simulated key press both give the same result:

   LMenu key down
   NumPad1 key down
   NumPad1 key up
   NumPad4 key down
   NumPad4 key up
   NumPad7 key down
   NumPad7 key up
   LMenu key up

I think should be some problems with the library: issue 1. Could anyone help me with this please? Is there any other way to do this?

Update 1

I found "Alt+Tab" is also not working in Win8. I thought this may be the same reason so I try to fix this first. It turns out they are two different problems:

  1. To make "Alt+Tab" working, I need to set uiAccess=true in "app.manifest" and sign the ".exe" file using a test digital signature;

  2. Simulating the input of ASCII characters still not working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
ray
  • 183
  • 4
  • 12

3 Answers3

0

Use keybd_event with MapVirtualKey or if using SendInput add to INPUT's Scan MapVirtualKey.

Here is example with both,

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace kbd_events_example
{
    static class Program
    {
        #pragma warning disable 649
        internal struct INPUT
        {
            public UInt32 Type;
            public KEYBOARDMOUSEHARDWARE Data;
        }
        [StructLayout(LayoutKind.Explicit)]
        //This is KEYBOARD-MOUSE-HARDWARE union INPUT won't work if you remove MOUSE or HARDWARE
        internal struct KEYBOARDMOUSEHARDWARE
        {
            [FieldOffset(0)]
            public KEYBDINPUT Keyboard;
            [FieldOffset(0)]
            public HARDWAREINPUT Hardware;
            [FieldOffset(0)]
            public MOUSEINPUT Mouse;
        }
        internal struct KEYBDINPUT
        {
            public UInt16 Vk;
            public UInt16 Scan;
            public UInt32 Flags;
            public UInt32 Time;
            public IntPtr ExtraInfo;
        }
        internal struct MOUSEINPUT
        {
            public Int32 X;
            public Int32 Y;
            public UInt32 MouseData;
            public UInt32 Flags;
            public UInt32 Time;
            public IntPtr ExtraInfo;
        }
        internal struct HARDWAREINPUT
        {
            public UInt32 Msg;
            public UInt16 ParamL;
            public UInt16 ParamH;
        }
        #pragma warning restore 649
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint extraInfo);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int MapVirtualKey(uint uCode, uint uMapType);
        [DllImport("user32.dll", SetLastError = true)]
        static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] inputs, Int32 sizeOfInputStructure);
        enum VK
        {
            MENU = 0x12,
            NUMPAD0 = 0x60,
            NUMPAD1 = 0x61,
            NUMPAD2 = 0x62,
            NUMPAD3 = 0x63,
            NUMPAD4 = 0x64,
            NUMPAD5 = 0x65,
            NUMPAD6 = 0x66,
            NUMPAD7 = 0x67,
            NUMPAD8 = 0x68,
            NUMPAD9 = 0x69
        }
        const uint KEYEVENTF_KEYUP = 0x0002;
        public const int INPUT_KEYBOARD = 1;
        [STAThread]
        static void Main()
        {
            Thread.Sleep(5000); //wait 5 seconds, so you can focus the Notepad
            keybd_event((int)VK.MENU, (byte)MapVirtualKey((uint)VK.MENU, 0), 0, 0); //Alt Press  
            keybd_event((int)VK.NUMPAD1, (byte)MapVirtualKey((uint)VK.NUMPAD1, 0), 0, 0); // N1 Press  
            keybd_event((int)VK.NUMPAD1, (byte)MapVirtualKey((uint)VK.NUMPAD1, 0), KEYEVENTF_KEYUP, 0); // N1 Release  
            keybd_event((int)VK.MENU, (byte)MapVirtualKey((uint)VK.MENU, 0), KEYEVENTF_KEYUP, 0); // Alt Release    
            Thread.Sleep(2000); //wait 2 seconds
            INPUT[] inputs = new INPUT[] {
                new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.MENU, Flags = 0, Scan = (ushort)MapVirtualKey((uint)VK.MENU, 0), ExtraInfo = IntPtr.Zero, Time = 0}}},
                new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.NUMPAD2, Flags = 0, Scan = (ushort)MapVirtualKey((uint)VK.NUMPAD2, 0), ExtraInfo = IntPtr.Zero, Time = 0}}},
                new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.NUMPAD2, Flags = 2, Scan = (ushort)MapVirtualKey((uint)VK.NUMPAD2, 0), ExtraInfo = IntPtr.Zero, Time = 0}}},
                new INPUT {Type = INPUT_KEYBOARD, Data = new KEYBOARDMOUSEHARDWARE { Keyboard = new KEYBDINPUT { Vk = (ushort)VK.MENU, Flags = 2, Scan = (ushort)MapVirtualKey((uint)VK.MENU, 0), ExtraInfo = IntPtr.Zero, Time = 0}}}
            };
            SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
        }
    }
}

As for InputSimulator, you need to write your own class using the SendInput or keybd_event with MapVirtualKey to handle inputs...

BladeMight
  • 2,670
  • 2
  • 21
  • 35
0

Cœur, what version of InputSimulator were you using. Version 1.0.4 has the ModifiedKeyStroke(IEnumerable modifierKeyCodes, IEnumerable keyCodes) method which should allow the following:

sim.Keyboard.ModifiedKeyStroke(new[] { VirtualKeyCode.LMENU }, new[] { VirtualKeyCode.NUMPAD1, VirtualKeyCode.NUMPAD4, VirtualKeyCode.NUMPAD7 });
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • This an example of some programm show some aspects how to use this library. InputSimulatorPlus - https://github.com/TChatzigiannakis/InputSimulatorPlus – Igor Nov 26 '22 at 23:21
0
    using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Threading;
using WindowsInput;
 


namespace WindowsMacros
{
    class Program
    {
        
        // import function in your class
        [DllImport("User32.dll")]
        static extern int SetForegroundWindow(IntPtr point);

        static string programnamepath = @"C:\Users\Murzic2\Desktop\Resize.exe — ярлык.lnk";
        static string FolderDeafault = @"C:\Users\Murzic2\Desktop\Resize";
        static void Main(string[] args)
        {
            MethodResizezing();
        }// End Main

        private static void MethodResizezing()
        {
            if (!Directory.Exists(FolderDeafault))
            {
                Directory.CreateDirectory(FolderDeafault);

            }
            Process process = Process.Start(programnamepath);

            Process[] processes = Process.GetProcessesByName("Resize"); // Programm Name
            Process resize = new Process();
            foreach (Process str in processes)
            {
                if (str.ProcessName.Contains("Resize"))
                {
                    resize = str;
                    Console.WriteLine(resize.ProcessName);
                }
                
            }
            if (resize != null)
            {
                Console.WriteLine("Begin programm into focus ...");
                IntPtr h = resize.MainWindowHandle;
                SetForegroundWindow(h);

               

                Console.WriteLine("Getting Out Of Programm");

                InputSimulator isim = new InputSimulator(); // From Library  InputSimulator
                if (resize.StartInfo.CreateNoWindow)
                {
                    Console.WriteLine(resize.StartTime);
                }


                Thread.Sleep(3000);// After need write according process stoped                      

                MouseMove(isim, 222, 83); // Move Mouse to Open Folder Window

                isim.Mouse.LeftButtonClick(); // Open  Directory Window Window 

                MouseMove(isim, 297, 269); // 

                isim.Mouse.LeftButtonClick();

                Thread.Sleep(200);
                isim.Keyboard.ModifiedKeyStroke(WindowsInput.Native.VirtualKeyCode.CONTROL, WindowsInput.Native.VirtualKeyCode.VK_A);// Select all Key Stroke Ctrl+A
                Thread.Sleep(200);
                isim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN); // Enter Key
                
                MouseMove(isim, 1468, 796);

                isim.Mouse.LeftButtonClick();
                Thread.Sleep(200);
                isim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN); // Enter Key

                Thread.Sleep(800);

            }

            Console.ReadKey();

            // open this code after finishing ))
            foreach (Process prc in processes)
            {
                if (prc.ProcessName.Contains("Resize"))
                {
                    prc.Kill();

                }

            }

        }

    private static void MouseMove(InputSimulator isim,double X, double Y)
        {
            //X = 222; --  Coordintes on scree`enter code here`n  which you need
            //Y = 83;  --  Coordinates on screen which you need
            Thread.Sleep(350);
            X = X * 65535 / 1535; // Converting trgarding yours screen 
            Y = Y * 65535 / 862;// Converting trgarding yours screen 
            isim.Mouse.MoveMouseTo(Convert.ToDouble(X), Convert.ToDouble(Y)); //
            Thread.Sleep(150);         


        }      

    }
}
Igor
  • 55
  • 1
  • 3