Click here to Skip to main content
15,867,308 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.

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;
             }
         }
 
Share this answer
 
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.
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.
 
Share this answer
 
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.
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>
 
Share this answer
 
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

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



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