Click here to Skip to main content
14,932,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to make Win+F3 for example open a form.

What I have tried:

C#
//global keyboard hook
static void gkh_KeyDown(object sender, KeyEventArgs e)
        {
 switch (e.KeyCode)
            {
                case Keys.F3:
                    if ((Control.ModifierKeys.HasFlag(Keys.LWin)) || (Control.ModifierKeys.HasFlag(Keys.RWin)))
                    {
                        e.Handled = true;
                        openForm();
                    }
Posted
Updated 3-Apr-21 12:23pm
v7
Comments
Richard MacCutchan 3-Apr-21 4:13am
   
Where is the rest of the code?
john1990_1 3-Apr-21 7:45am
   
My apologizes, I updated the question now, please check it.
Richard MacCutchan 3-Apr-21 10:42am
   
Having tried this it seems that the Win keys are not passed to user applications.

Well, your question sucks because you don't mention if one of the keys works, and one of them doesnt. The reason I say this is because based on the code you posted, you're checking Keys.LWin twice. If that's really what your code is doing, you'll never know when Keys.RKey is pressed.

Furthermore, if you press the one of the windows keys, doesn't Windows display the start menu? How can you possibly think using a key combination that involves the windows key is a good idea?

EDIT=================

If you insist on pursuing this, look at WPF KeyBindings

XAML
<Window.InputBindings>
    <KeyBinding Key="F5"
                Command="{Binding MessageCommand}"
                CommandParameter="You pressed 'F5'"/>
</Window.InputBindings>
   
v2
Comments
john1990_1 3-Apr-21 7:31am
   
My apologizes, I updated the question now, please check it.
#realJSOP 3-Apr-21 7:45am
   
Once again, what makes you think using a key combination that includes the Windows key is in any way a good idea? You won't do anything but confuse/anger your users. Typically, you should use Ctrl or Shift keys for this kind of thing.
john1990_1 3-Apr-21 7:47am
   
Many programs use the Win key, for example I know of Ditto.
#realJSOP 3-Apr-21 7:57am
   
I've been a programmer for over 40 years, and have NEVER seen an app that uses the Windows key in a key combination (other than Windows itself, or one of its own apps). I consider the Windows key to be for the sole use of the operating system.

You should visit this page to see why using the Windows key is a minefield of frustration and failure:

https://www.windowscentral.com/best-windows-10-keyboard-shortcuts

From a user interface point of view, keyboard shortcuts are generally invisible to the user. Invisible UI commands just annoy the end user. I personally avoid them whenever possible.
john1990_1 3-Apr-21 8:13am
   
Thx, you are right, but still I want this option in settings of my program as I have seen it in Ditto program as in this screenshot:
https://prnt.sc/11346r5
#realJSOP 3-Apr-21 8:32am
   
I updated my answer.
john1990_1 3-Apr-21 8:34am
   
Thx, I'm sorry I know only C# (and HTML) in WinForms and Console.
#realJSOP 3-Apr-21 9:21am
   
Ahhh, I thought we were talking about was WPF. If it's WinForms, just ignore me. I stopped doing WinForms in 2009
john1990_1 3-Apr-21 9:17am
   
Also the code you provided seems to be about F5 and not WinKey.
#realJSOP 3-Apr-21 9:21am
   
It was just an example.
Dave Kreskowiak 3-Apr-21 12:52pm
   
So, because Ditto made that mistake, that makes it OK for you to make it too?

Microsoft reserves the right to keep any Winkey combination for itself, breaking your application, without notifying you ahead of time.

You've been warned.
john1990_1 3-Apr-21 14:58pm
   
Ok, thx, I still want my users to be able and have the option like in Ditto and some programs, to use WinKey in their hotkey, would you please tell me how to do it?
Dave Kreskowiak 3-Apr-21 14:59pm
   
Did you really read the article at https://www.windowscentral.com/best-windows-10-keyboard-shortcuts ???

Did you notice all of the Winkey combinations you CANNOT use in your app?
john1990_1 3-Apr-21 15:01pm
   
I can use global for example WinKey+F1 to open my winform.
BillWoodruff 3-Apr-21 23:27pm
   
+5
In Windows Forms, the Win keys are not modifier keys like Shift, Alt, and Ctrl.

You have to track the state of the Win keys yourself by setting your Form's KeyPreview property True, then handling the Form's KeyDown and KeyUp events, checking for the key that's pressed or released, seeing if it's either Win key, then setting a class-level flag for the key being pressed or released.

When you get a non-Win key press in the event handlers, you check for the state of the Win key flag to set. If it's down, you can assume Win-whatever was pressed and released.


Keep in mind that this will NOT prevent the system from seeing the key presses before your app does and the system WILL act on them if pressed.

So if you want to use Win-F1, be prepared for some kind of Windows 10 Help browser tab to launch.
   
v2
Comments
john1990_1 3-Apr-21 18:14pm
   
Thanks, I used to do this way but maybe it wasn't working well, like sometimes the global keyboard hook doesn't successfully register a keydown or keyup of Ctrl for example and I had issues with it when the form opened when I didn't want it to open (Ctrl was registered as down as keyup wasn't run for it). Anyway I will give you 4 stars and accept the answer.
Dave Kreskowiak 3-Apr-21 18:19pm
   
You never said this was a global keyboard hook.

Skip everything about Forms and Key preview. Everything else is the same. You have to track the make/break status of the keys yourself.
john1990_1 3-Apr-21 18:23pm
   
Please see my code in my answer...
john1990_1 3-Apr-21 18:39pm
   
On the global keyboard hook of keydown on F1-F12 I make e.Handled=true; if WinKey was as the user choose (pressed/not pressed) and my form opened. So I don't get a help window open on F1 for example.
Dave Kreskowiak 3-Apr-21 18:45pm
   
Global Keyboard Hooks change the game. You have control over whether the keys make it to the system or not, simply by not passing them down the hook chain.

But, you never said anything about a global keyboard hook in your original question.
john1990_1 3-Apr-21 18:46pm
   
Yes, sorry, my mistake.
BillWoodruff 3-Apr-21 23:27pm
   
+5
The answer:

static bool isLeftWinDown = false;
static bool isRightWinDown = false;

        static void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.LWin: isLeftWinDown = true; break;
                case Keys.RWin: isRightWinDown = true; break;
            }
        }


static void gkh_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.LWin: isLeftWinDown = false; break;
                case Keys.RWin: isRightWinDown = false; break;
             }
         }
   
Comments
BillWoodruff 3-Apr-21 23:25pm
   
my vote of #1: after not telling us you were using a Global Keyboard Hook ... wasting our time ... you now post this incomplete code which does not handle F3.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900