C
C#10mo ago
MotherBroker

✅ 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
qqdev
qqdev10mo ago
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
MotherBroker
MotherBrokerOP10mo ago
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 ?
qqdev
qqdev10mo ago
Wait. Your form is transparent
MotherBroker
MotherBrokerOP10mo ago
no only the panel
qqdev
qqdev10mo ago
Do you have a screenshot?
MotherBroker
MotherBrokerOP10mo ago
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
No description
qqdev
qqdev10mo ago
And you want to capture inputs within that transparent part? Or in general
MotherBroker
MotherBrokerOP10mo ago
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)
leowest
leowest10mo ago
out of curiosity what is that for? like what are u capturing etc
MotherBroker
MotherBrokerOP10mo ago
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
leowest
leowest10mo ago
even if the form is not in foreground? or it will be always in foreground
MotherBroker
MotherBrokerOP10mo ago
the form is topmost so you will always have this box to see the output and what you're capturing
leowest
leowest10mo ago
top most but not active window I guess if that is even possible
MotherBroker
MotherBrokerOP10mo ago
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
leowest
leowest10mo ago
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?
MotherBroker
MotherBrokerOP10mo ago
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
leowest
leowest10mo ago
well if the for is always active, this is winforms?
MotherBroker
MotherBrokerOP10mo ago
wdym this is windforms ?
leowest
leowest10mo ago
nvm u said in your question windows forms
qqdev
qqdev10mo ago
Yeah, I don't think you can click through while also getting notified about the input. Global stuff needed
MotherBroker
MotherBrokerOP10mo ago
The form doesn't have the focus when i click through
leowest
leowest10mo ago
yeah then it wont be trivial
MotherBroker
MotherBrokerOP10mo ago
Damn, goofy c#
leowest
leowest10mo ago
I mean its not c# thou its just how windows work
MotherBroker
MotherBrokerOP10mo ago
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
qqdev
qqdev10mo ago
Maybe, but it is veeeeery specific And not sure if possible at all
MotherBroker
MotherBrokerOP10mo ago
Yes but global key capturing is pretty common 😦
leowest
leowest10mo ago
but u can probably track the purple part let me try
MotherBroker
MotherBrokerOP10mo ago
Alright thanks
leowest
leowest10mo ago
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
qqdev
qqdev10mo ago
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
MotherBroker
MotherBrokerOP10mo ago
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 ?
qqdev
qqdev10mo ago
I don't know any language with a standard lib which has that
leowest
leowest10mo ago
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
label2.Text = e.Location.ToString();
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
label2.Text = e.Location.ToString();
}
so basically it will update a label with the mouse position within the panel
qqdev
qqdev10mo ago
That's AutoHotkey/AutoIt stuff
leowest
leowest10mo ago
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
MotherBroker
MotherBrokerOP10mo ago
Yeah outdated asf lol Transparencykey
leowest
leowest10mo ago
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?
MotherBroker
MotherBrokerOP10mo ago
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
leowest
leowest10mo ago
TransparencyKey = Color.Transparent;
ForeColor = Color.Transparent;
TransparencyKey = Color.Transparent;
ForeColor = Color.Transparent;
?
MotherBroker
MotherBrokerOP10mo ago
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
leowest
leowest10mo ago
oh in WPF its very simple
MotherBroker
MotherBrokerOP10mo ago
Form1 part :
No description
leowest
leowest10mo ago
oh magenta
MotherBroker
MotherBrokerOP10mo ago
panel1 part :
No description
MotherBroker
MotherBrokerOP10mo ago
it's backcolor not foreground mb
leowest
leowest10mo ago
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
MotherBroker
MotherBrokerOP10mo ago
with win api also ?
leowest
leowest10mo ago
capturing those with win api is not hard
MotherBroker
MotherBrokerOP10mo ago
Both are captured by winapi ?
leowest
leowest10mo ago
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
MotherBroker
MotherBrokerOP10mo ago
Okay, thanks mate i'll dive right in I've never done so much for key capture 🤣
leowest
leowest10mo ago
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
MotherBroker
MotherBrokerOP10mo ago
Gotta check the NuGets
leowest
leowest10mo ago
or something
MotherBroker
MotherBrokerOP10mo ago
thanks ❤️
leowest
leowest10mo ago
no worries gl 🙂 do a /close if u dont mind 😉
Want results from more Discord servers?
Add your server