✅ Global keypress Simulation?
hello, i have a project where i need to perform global mouse and key Presses, i cant seem to find a way, or Library that can do that, i need to read an array of strings, to perform the presses, and i need to be able to simulate holding down the key and mouse for an amount of time, do you know ways or Librarys to do that? i programm in VS and use windows forms c#. (something like this https://www.nuget.org/packages/H.InputSimulator#readme-tab (i cant get said extra functions to work here))
14 Replies
this just ends up using win32 APIs for input https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput
What exactly isn't working?
SendInput function (winuser.h) - Win32 apps
Synthesizes keystrokes, mouse motions, and button clicks.
i cant find a way to convert a string into the right format to send the key, and i cant get the key to be held down to get something like "llllllllllllllllllllllll"
i need something like this to work or be abe to convert it to the right format
I don't know much about the library, but I would expect something like this to work
That said, if it doesn't work you may have to keep sending the entry at an interval.
I think i could make this work with a timer, but i still need so be able to send the input in some way starting with a String, because otherwise i would need to write code for every key and i dont want to do that
i think i found somethink that could work, i found the keyword enum and there is something calles enum.TryParse() but i cant figure out how to use it. (and im not even sure if it does what i want it to do)
You shouldn't need to parse them if you know the key at time of code
i dont know the key, my overall goal is to write a program that records a macro and plays it, i have the record part but i cant find a dynamic way to replay the array of keys, i would need to write a function for every key, but i think there shoul be a more elegant way to solve this problem?
this is an example of a recorded macro i know how to read the file and convert the line into the right line( for example: Pressed: L to "VK_L") but i dont know how to convert this into the WindowsInput.VirtualKeyCode or whatever the format is called?
The Api has a way to do text entry https://github.com/HavenDV/H.InputSimulator/blob/master/src%2Flibs%2FH.InputSimulator%2FKeyboardSimulator.cs#L297
GitHub
H.InputSimulator/src/libs/H.InputSimulator/KeyboardSimulator.cs at ...
Allows you to simulate global mouse and keyboard events. - HavenDV/H.InputSimulator
If that doesn't work and you need more fine grained here is how they construct the input list https://github.com/HavenDV/H.InputSimulator/blob/master/src%2Flibs%2FH.InputSimulator%2FInputBuilder.cs
GitHub
H.InputSimulator/src/libs/H.InputSimulator/InputBuilder.cs at maste...
Allows you to simulate global mouse and keyboard events. - HavenDV/H.InputSimulator
thanks, this might be what i need, im going to test some things with it.
i've figured out how to get from string to virtualkeycode format wich turn out to be a enumeration, and ive learned what enumerations are.
this was the solution to my problem
now i need to figure out how to get the key to be held down, bc i think if i simulated the held down keypress by using timers and repeating the input it wouldnt be the same as actually the held down key