❔ ✅ Is there a Way to extend this code from this yt tutorial
so ive been following this yt tutorial https://www.youtube.com/watch?v=nTKTPqqFtrk, which shows a way to implement a keyboard hook(i think "^^) and it does work so far, but i was wondering if i could extend the code to show me which button is pressed and released rather than just givig me a messagebox with the notofication.(i apologize if my question is written poorly)
here is my code:
tez kidd
YouTube
C# Global Hotkeys
Setting up Global Hotkeys in C# Win Forms
By my mistake, I accidentally added the imports ReleaseCapture & SendMessage. You do not need these.
123 Replies
and the class:
if by "extend" the code, you mean "modify" it, then sure
yes sorry i mean modify :)
There are a number of Nuget Packages available that can already do this if you want a more structured approach, H.Hooks features an event based notification for when a key is pressed Down and Up and whether to intercept the keys (dont send them to other applications).
There's also the GlobalHotkey approach from StackOverflow which has a sort of "register a callback" approach where you declare something like
or in your case
Additionally there's the advanced approach of doing WinAPI calls for low level keyboard procs which removes the need for your application in most cases needing to be on the same (or higher) elevation level as the focused application.
Example use of H.Hooks is
Also, looking at the code you provided before (and some StackOverflow), if you'd want the key you can do this in your WndProc:
Thx for the help im going to look into it when i have some time :)
Btw im a beginner incase that wasnt clear, so i will probably need aome time to understand
woah ive been reading your comments and i must say im truly thankfull arion <3
ive been looking into this H.Hooks and i would like to try it but i dont know how to use it, there is this install package thing but i dont know where to use it "^^?
Thats one way to install a package, another is using Visual Studio's nuget package manager
most of your nuget packages will likely be installed this way
thanks you are a great help <3
ive copied your code and got this error...
so ive modified the code with the help of chat gpt, but when i launch it now i get this error message, would you mind helping me further?
can you translate that?
oh, since its a winforms app, just remove the Console.ReadKey line
for these:
you can even create methods for them if you want
like:
its likely something like, the app isnt accepting std-in or is not a designated console app so therefore InvalidOperationException
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.sorry for not replying for so long i was busy
ive intalled the english language model and chaged it to english but for some reason the error is still in german i already restatet vs idk how to translate it?
I mean, do you read German? You seem to speak English.
german is my native language but ofc i use english to write with you guys bc not everyone speaks german :)
but i take that as a compliment :D
anyway, the exception being in German is a result of .NET, not Visual Studio
I dunno if it's a setting you could change, without having to reinstall an English verison of .NET, or if it's based on your system language settings
this is how my code looks so far what does the "?" do?.
and how can i get the key ive typed to go in my messagebox?
it looks like this error is in english so the langauge thing seems to be fixed :)
?
can mean a lot of things, but when attached to a type specification like that, it indicates that the type is allowed to be null
specifically, because it's attached to a reference type, it's a metadata annotation for the compiler, and is basically meaningless to code functionality
as the error indicates, you are not using a version of the C# language that supports that feature
it was added in C#8
you're using C#7.3oh i guess i should update my c#
you cannot annotate reference types as nullable or non-nullable, all reference types are assumed to always be nullable
or remove the annotation
i think i want to update, there is probably a reason that this was added so sooner or later i might need it anyway
there is indeed a reason
but how can i get the key i typed to into the messagebox?
it allows the compiler to better analyze your code for flaws
it allows the compiler to do this....
thus allowing you to either fix it with a null check
or change the annotation, if your intention is that
target
should never be null
THAT means that the compiler will warn you if you try and CALL DoSomething()
with a value for target
that it thinks could be null
etc. etc.
okay, so....
you are typing a key into a messagebox?thanks for the explanation,
i want to show me the key once i pressed it in a messagebox and later in a textbox for now the messagebox will do
what messagebox?
oh, wait
okay
opposite of what you said
so, you want to CAPTURE a keypress
and display it later
yep
im going to update my c# later for now im going to remove the "?"
i figured it out
gg
is there a limit to wich key can be captured or is every key "captureable"?
is there a limit to wich key can be capturednot really, no
oh thats nice
so when i want to write my text into a textbox i get this exception
chatgpt translation: "System.InvalidOperationException: 'Invalid cross-thread operation: Access to control textBox1 was performed from a thread other than the one it was created for.'"
error says it all
this seems to fix it, but i dont understand why, but u guess as long as it works i should be fine
Invoke()
marshals an action onto the thread that owns the control
I.E. your Form
which is precisely what you want
the global key handler you registered is invoked on a thread entirely outside of your application, managed by the OSoh thanks
i am going to need some of this stuff in school and present and explain it for a grade, this is going to be interesting xD
okay this is everything i wanted to know i guess, now im going to implement the mouse (this should be similar), and in the future i want to replay the inputs but im going to do that far in the future
you dont necessarily need to add
new Action(
but instead
so this, becomes this:
oh okay, what does the new action part do?
uh, it calls itself.
()=> {};
is essentially an action, so doing that twice wont help muchuhh something went wrong xD
i think i leave the code as it is, bc im not realy sure what happening
What does the error say?
since it works on mine
What does "Show potential fixes" say?
because it doesnt complain on my side:
do you mean this?
how can i view potentioal fixes?
try this option?
now i got this
@V.EINA Jaken Any ideas?
what is the error there?
same as befor
I'm not sure why the compiler can't infer the delegate type there, but it obviously can't
.Invoke()
has two different overloads that could fit that signature
since Action
actually derives from Delegate
the compiler ought to give precedence to the Action
overload, but it's trying to bind to the Delegate
one instead
and it doesn't know what type of delegate to create
put new Action()
back
whatever the combination of reasons, Roslyn isn't smart enough to infer the delegate type here, you need to specify it manuallyimma try booting up my Visual Studio and see if its a VS issue
cuz im using Rider most of the time
also, to answer the earlier question,
new Action(() => something())
and () => something
are functionally and semantically identical
the second is simply shorthand for the first, when the compiler is smart enough to infer that an Action
is neededhmmm, are u using Visual Studio 2017 or earlier by any chance?
no
@RohesKätzchen
oh, him
xD
actually, he's using C#7.3, established earlier
that could explain the lack of inferrability
i think visual studio 2022
drop your lang version to 7.3 arion
how can i change the lang version, couldnt figure it out earlier?
in your csproj file
I get nullability errors but nothing else
when dropping to 7.3
hmmm
remove the brackets?
still no error
odd
:/
@RohesKätzchen if you put a
new Action()
wrapper back around that, does that eliminate the error?yes
@arion you're testing against WinForms?
yeah, from my csproj:
I mean, like, that
.Invoke()
calls is System.Windows.Forms.Control.Invoke
?yeah
@RohesKätzchen what framework are you running?
Since its to method body it changes from Action to Func<string>
wait, why the hell is it resolving to the
Func<T>
overload?setting the string returns the string
uhh windows forms? this one
yeah
There we have the problemo
xD
not really a problem, just the reason that you're getting different behavior
huh, i dont understand anything whats going on xD
you're running .NET Framework
arion's running on .NET 5+
the
Action
and Func<T>
overloads of .Invoke()
don't exist for you
only the Delegate
overload
so you literally HAVE to specify the delegate typeoh okay
Basically .NET Framework is from like 10-20 years ago, .NET Core is more modern
somewhat
it's just a different version, different stuff exists
btw, thank both of you for all the help, you realy saved me <3
Regarding this btw, it transforms Action to Func<string> xD
arion
REPL Result: Success
Console Output
Compile: 596.034ms | Execution: 75.072ms | React with ❌ to remove this embed.
yeah
Weird interaction
how hard will it be to simulate mouse and key inputs outside of the application?
simulate?
like, for testing?
like i can record my inputs and later on in my project i want to ,,play,, them like a macro
hard
damn, i should have taken a easier project for school :(
it rather goes against one of the OS's core jobs of keeping separate programs from interfering with one another
same as capturing global key input, you're going well outside of the realm of C# to generate global key input
do you think there is another nuget library for that, bc that made it a lot easier?
well thats a diffrent topic ig, so for now all my questions are answered. i probably will open another topic about simulating input in the near future
again huge thanks to you <3
so if you are also done, i would /close this thread
can i still read it after i closed it?
From purely looking at the readme here https://www.nuget.org/packages/H.InputSimulator#readme-tab this might work
possibly
yes
well i will look into this
so ig thats it
kind of emotional for me to close it :(
well
bye 🥲
You can always create additional help posts if you're stuck on anything else
i will 👋
ayo incase one of you is still reading how can i get the scroll event for my mouse in H.Hooks?
if you want to send the scroll keys:
3 being the amount of scrolls upwards i believe
thanks a lot <3
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.