✅ Detect key press outside of the form
Hello i'm working with c# windows forms. My form has a transparent panel that i can click and see through. How can i capture the key pressings when not focused on the main form ??
57 Replies
Used to accomplish that with a global keyboard hook
Not sure if that's still the way to go
Spoiler: It is kinda nasty when you are not familiar with that kind of stuff
I've seen both global keyboard hook as well as GetAsynckeystate dll import from user32 but isn't there actually a simpler way ? in c++ there's already the GetAsyncKeyState function ?
Wait. Your form is transparent
no only the panel
Do you have a screenshot?
purple part is the panel that's transparent and black part is a picturebox. The red part is the main form that's not transparent
And you want to capture inputs within that transparent part?
Or in general
It's like a cardboard box with a rectangle hole and you can see what's behind it. The picturebox displays modified images with OpenCV of what is inside the purple part. I need to still be able to click and see through but at the same time capture which key is being pressed
(if it ain't so clear i'll try to re-explain lol)
out of curiosity what is that for?
like what are u capturing etc
It's like a "screenshare" but the picturebox will display the shapes that OpenCV can detect (circle in my case)
And i want to detect where is my mouse cursor when i click in the purple part
even if the form is not in foreground?
or it will be always in foreground
the form is topmost so you will always have this box to see the output and what you're capturing
top most but not active window I guess
if that is even possible
And i need to retrieve the X, Y coords of the mouse when i press a key (or mouse click)while in the purple part
I guess u would have to globaly track the mouse and use your window position minus screen size to get he mouse position I wonder if there is anything that facilitates that
I suppose u already tried tracking it from the form itself right?
I didn't understand sorry lol, i didn't try any stuff for now cause i want a simple solution and not use overcomplicated stuff
the code is already a bit heavy and i don't want my brain to melt for capturing inputs xd
well if the for is always active, this is winforms?
wdym this is windforms ?
nvm u said in your question
windows forms
Yeah, I don't think you can click through while also getting notified about the input. Global stuff needed
The form doesn't have the focus when i click through
yeah then it wont be trivial
Damn, goofy c#
I mean its not c# thou
its just how windows work
Yeah true but there should be some stuff made to avoid situations like this
Thanks for the help guys, i'm gonna do my research about user32 getasynckey
Maybe, but it is veeeeery specific
And not sure if possible at all
Yes but global key capturing is pretty common 😦
but u can probably track the purple part
let me try
Alright thanks
yeah you can use MouseMove event on the panel
it still captures it even with the window not being active but topmost
so all u would need is a win32api to get the left mouse press
and store both
so it would be that difficult
That's true. I think it's hard to bring into any offical .NET codebase because you would have to make it cross-plat and WinForms, etc. don't get that many new features (lol) ;D
So you said i can retrieve the mouse coords even if transparent but for the detection of key i need to go with something different ?
I don't know any language with a standard lib which has that
so basically it will update a label with the mouse position within the panel
That's AutoHotkey/AutoIt stuff
I did not try transparent I haven't used winform is like 7 years
so I dont even remember how to make the window transparent
Yeah outdated asf lol
Transparencykey
I mean its good to moc up something if u have no GUI knowledge in c#
but if u do I much rather work with WPF
on the form it self or panel?
I use it also but i have a lot to learn for now so i go with what i feel the most comfortable with ahah
In the window form set the transparency key to X color and put the panel foreground color to X
?
This is why i didn't chose wpf as i didn't succeed to change the transparent background lol
hold on i'll screen for you
oh in WPF its very simple
Form1 part :
oh magenta
panel1 part :
it's backcolor not foreground mb
ah ok yeah when its invisible it no longer can track the mouse so yeah u would have ot track the mouse relative position to your form panel
and the key press with win api
with win api also ?
capturing those with win api is not hard
Both are captured by winapi ?
what will be annoying is that u need to update your window position, substract it from the actual monitor resolution and also take into account the panel position in your window
yeah u can get both from it
Okay, thanks mate i'll dive right in
I've never done so much for key capture 🤣
there is very likely libraries that do that for u out of the box as nuget u can leverage too
which would resume the whole code in 3 lines
Gotta check the NuGets
or something
thanks ❤️
no worries gl 🙂
do a /close if u dont mind 😉