disable keys

Is there away to disable keyboard's \ mouse button(s) in web applicaiton and windows applicaiton..

using .net 1.1 .. please point me to the right API/dll/ examples I could use.

Than you in advance..



Answer this question

disable keys

  • Helge Norvang

    We had to solve this same problem in our application, and we found that we had to write a low level application that intercepted the key events before our main program got them, we did this using C++.


  • Scott McKeown

    Thank you all for your help,

    I kinda understand now, I will research more ( on VKCode.. etc) ..to find the final answer..


  • GethWho

    JMyster wrote:

    I don't know what you are trying to use it for but this is what I use in one of my windows apps to only allow Number keys and the backspace key to be pressed. OnKeyPress Event

    private void NumberOnlyValues(object sender, System.Windows.Forms.KeyPressEventArgs e)

    {

    if (char.IsNumber(e.KeyChar) | (e.KeyChar == '\b'))

    {

    }

    else

    {

    e.Handled = true;

    }

    }



    JMyster, he is trying to stop keys from being pressed to other applications, not just the windows form, although I wish it was that easy.

    Sanophy

  • Big5824

    Hi IMBack, I remember reading an article by Stephen Toub about this issue... heres the class:

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    class InterceptKeys
    {
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    public static void Main()
    {
    _hookID = SetHook(_proc);
    Application.Run();
    UnhookWindowsHookEx(_hookID);
    }

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
    using (Process curProcess = Process.GetCurrentProcess())
    using (ProcessModule curModule = curProcess.MainModule)
    {
    return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
    GetModuleHandle(curModule.ModuleName), 0);
    }
    }

    private delegate IntPtr LowLevelKeyboardProc(
    int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(
    int nCode, IntPtr wParam, IntPtr lParam)
    {
    if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
    {
    int vkCode = Marshal.ReadInt32(lParam);
    Console.WriteLine((Keys)vkCode);
    }
    return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
    LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
    IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
    }

    This is a low level keyboard hook for the entire system... to disable a key from being sent to the system, dont return CallNextHookEx and you should be fine.

    The entire article can be found here:
    http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx







  • PublicError

    Hi again IMBack,
    You will use the same methods from user32 to prevent the sleep/power keys from being passed. All you need is the VKCode of the sleep and power keys. VKCode = Virtual Key Code, check msdn for it.

    Sanophy

  • mklaey

    Thanks,

    What dll I should use to control the sleep/power keys from being pressed.. Could you explain what you mean by a driver that sits between keyword and windows drivers..


  • Sammy1971

    You can't prevent keys/mouse buttons from being pressed in a web application in fact if you could it would be very very very bad, web apps shouldn't ever have this control over the operating system.

    Also this may or may not be an issue to you but if you want to prevent the sleep/power keys from being pressed you are going to have right a driver that sits between the keyboard and windows keyboard drivers, and it will have to intercept these buttons. And you will have to do different work for USB keyboards as opposed to PS1 keyboards.


  • IceAngel89

    I don't know what you are trying to use it for but this is what I use in one of my windows apps to only allow Number keys and the backspace key to be pressed. OnKeyPress Event

    private void NumberOnlyValues(object sender, System.Windows.Forms.KeyPressEventArgs e)

    {

    if (char.IsNumber(e.KeyChar) | (e.KeyChar == '\b'))

    {

    }

    else

    {

    e.Handled = true;

    }

    }


  • Billl

    What do you mean low level application..

    If it is not long. could you share how you did it.. so maybe I could convert it to C# or invoke c++ file in c#, if it is possible.

    Thank you,


  • JMBC

    Not reliably. You could write a global mouse/keyboard hook application to filter mouse/keyboard messages...

  • whatthedilly

    Unfortunatly it's not quite as easy as catching the VKCode of the sleep and power keys on the keyboard to prevent the computer from shutting down as these are handled differently by windows. You have to write a filter driver that sits in between the keyboard and windows keyboard driver to intercept those keys and prevent them from being executed.
    You also have to write two different drivers one for a ps2 keyboard and one for a usb keyboard. If anybody has found another way to do this I would love to here about it because I haven't been able to get anything else to work.

    How to disable the keyboard Sleep button with a filter driver

    http://support.microsoft.com/kb/302092

  • disable keys