Disable some keys in keyboard

Question

Thursday, January 16, 2014 12:38 PM

Is there anyway to disable some keys in keyboard using visual c# ?

I want to disable all the functions keys because some of them do some tasks by default in windows. For example if I press the F1 key windows would show the help window. I want make this not to happen for my program. That program is about converting Function keys to Multimedia keys. What is the easiest way to do this ?

All replies (13)

Friday, January 17, 2014 10:43 AM âś…Answered | 1 vote

In that case you must use Keyboard Hooks to accomplish your job.

Here are some articles which can help

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959%28v=vs.85%29.aspx

http://www.codeproject.com/Articles/1264/KeyBoard-Hooks

http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook

The last article has a demo project in C#, but if you anything complicated then let me know.


Thursday, January 16, 2014 1:00 PM | 1 vote

If you are using windows forms you can check this - How to: Modify Keyboard Input to a Standard Control

// Detect F1 through F9 during preprocessing and modify F3. 
public override bool PreProcessMessage(ref Message m)
{
    if (m.Msg == WM_KEYDOWN)
    {
        Keys keyCode = (Keys)m.WParam & Keys.KeyCode;

        // Detect F1 through F9. 
        switch (keyCode)
        {
            case Keys.F1:
            case Keys.F2:
            case Keys.F3:
            case Keys.F4:
            case Keys.F5:
            case Keys.F6:
            case Keys.F7:
            case Keys.F8:
            case Keys.F9:

                MessageBox.Show("Control.PreProcessMessage: '" +
                  keyCode.ToString() + "' pressed.");

                // Replace F3 with F1, so that ProcessKeyMessage will   
                // receive F1 instead of F3. 
                if (keyCode == Keys.F3)
                {
                    m.WParam = (IntPtr)Keys.F1;
                    MessageBox.Show("Control.PreProcessMessage: '" +
                        keyCode.ToString() + "' replaced by F1.");
                }
                break;
        }
    }

    // Send all other messages to the base method. 
    return base.PreProcessMessage(ref m);
}

You can try something similar as above code and write the methods in the respective key press.

Kunal G


Thursday, January 16, 2014 2:08 PM

This code works globally or does this work only with windows form ? I mean while running this program if user pressed F1 key,  windows wont shows the help window ?


Thursday, January 16, 2014 2:17 PM

By the way I tried your code.. And here is my code..

public override bool PreProcessMessage(ref Message m)
        {
            if (m.Msg == WM_KEYDOWN)
            {
                Keys keycode = (Keys)m.WParam & Keys.KeyCode;
                if (keycode == Keys.F1)
                {
                    m.WParam = (IntPtr)Keys.None;
                }
            }
        }

But this didnt work. And what does the "WM_KEYDOWN" means ? I dont know about that. So I tried this too..

public override bool PreProcessMessage(ref Message m)
        {
            if (m.Msg == Form1_KeyDown)
            {
                Keys keycode = (Keys)m.WParam & Keys.KeyCode;
                if (keycode == Keys.F1)
                {
                    m.WParam = (IntPtr)Keys.None;
                }
            }
        }

But none of them works. Whats the wrong with my code ?


Thursday, January 16, 2014 2:59 PM | 1 vote

It will work with Windows Forms only as it makes use of various events provided by windows forms (eg: KeyPress, KeyDown). Please refer this link for details. An excerpt from this link:

To modify a noncharacter key

  • Override a Control method that processes Windows messages, detect the WM_KEYDOWN or WM_SYSKEYDOWN message, and set the WParam property of the Message parameter to the Keys value that represents the new noncharacter key.

    The following code example demonstrates how to override the PreProcessMessage method of a control to detect keys F1 through F9 and modify any F3 key press to F1. For more information on Control methods that you can override to intercept keyboard messages, see User Input in a Windows Forms Application and How Keyboard Input Works.

 // The following Windows message value is defined in Winuser.h. 
        private int WM_KEYDOWN = 0x100;

That code in the previous response just shows one important method which you can modify. If you want full code refer Example section in the above link.

Kunal G


Friday, January 17, 2014 7:51 AM

Can you please post the full code to stop the F1 key action (showing the help) and send another key instead of it ? Im very new to c#. I really couldn't understand your answers.


Friday, January 17, 2014 8:49 AM

Hi

Do you want function keys to be disable for only your window?

Bcoz winforms in dotnet has no inbuilt functionality for function keys unless you specify one.

Can you elaborate.?


Friday, January 17, 2014 9:05 AM

No. Not only for my window. I want to disable or send some other key instead of them like Keys.None

So how can I do this easily ? I tried above codes and they didn't work.


Friday, January 17, 2014 9:28 AM

Let me explain what I understand by your thread

You want to capture a keyboard input before they are processed by any application or windows os and modify the input and send it to application or windows os.

Eg:

You want to capture the F1 key input before any application or os and modify it to say something like Alt+F4 and send this modified key input to further application or os.

I'm right or I need to be corrected.?


Friday, January 17, 2014 10:31 AM

Yes. That's what I want to do.


Friday, January 17, 2014 10:45 AM

Thanks.. Ill check them


Friday, January 17, 2014 11:05 AM

Thank you very much..

http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook

This solved the problem.. :)


Friday, January 17, 2014 11:06 AM

I'm glad for you.