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