2010-07-09 10 views
9

Puede alguien por favor dígame cómo deshabilitar las teclas de cambio de tarea usando C#Cómo suprimir teclas cambio de tarea (winkey, Alt-Tab, Alt + Esc, Ctrl-esc) con bajo nivel de enlace de teclado en C#

+1

¿Por qué querrías hacer esto? – Fosco

+4

Cualquier tipo de aplicación de kiosco ... Pregunta válida, no estoy seguro de por qué el DV, sin embargo, este es probablemente un duplicado. –

+0

La mayoría de los juegos de PC hacen esto. La pantalla completa exclusiva obtiene un mejor rendimiento en muchos sistemas, y ¿quién quiere que aparezca el chat de Skype mientras intentas jugar tu juego favorito? – yoyo

Respuesta

15

Tengo el código completo para desactivar la tecla de Windows ,Alt + Tab y así sucesivamente ..

Y ahora estoy proporcionando el siguiente código como referencia para los demás:

/* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Starts Here */ 

    // Structure contain information about low-level keyboard input event 
    [StructLayout(LayoutKind.Sequential)] 
    private struct KBDLLHOOKSTRUCT 
    { 
     public Keys key; 
     public int scanCode; 
     public int flags; 
     public int time; 
     public IntPtr extra; 
    } 
    //System level functions to be used for hook and unhook keyboard input 
    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool UnhookWindowsHookEx(IntPtr hook); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp); 
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern IntPtr GetModuleHandle(string name); 
    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    private static extern short GetAsyncKeyState(Keys key); 
    //Declaring Global objects  
    private IntPtr ptrHook; 
    private LowLevelKeyboardProc objKeyboardProcess; 

    private IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp) 
    { 
     if (nCode >= 0) 
     { 
      KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT)); 

      // Disabling Windows keys 

      if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)  
      { 
       return (IntPtr)1; // if 0 is returned then All the above keys will be enabled 
      } 
     } 
     return CallNextHookEx(ptrHook, nCode, wp, lp); 
    } 

    bool HasAltModifier(int flags) 
    { 
     return (flags & 0x20) == 0x20; 
    } 

    /* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Ends Here */ 

Luego dentro de Form_Load();

private void Form_Load(object sender, EventArgs e) 
    { 
     ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule; 
     objKeyboardProcess = new LowLevelKeyboardProc(captureKey); 
     ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0); 
    } 
+0

Recibo este error Error \t 'System.Windows.Input.ModifierKeys 'es un' tipo 'pero se usa como una' variable' – Omar

+1

Terminé usando esta verificación' if (objKeyInfo.key == Keys.RWin || objKeyInfo .key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier (objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (Keyboard.Modifiers & ModifierKeys.Control)! = 0) ' – Omar

+0

Utilizo este código, pero obtengo el error 'Intento leer o escribir en la memoria protegida. Esto a menudo es una indicación de que otra memoria está corrupta ' – Niloo

5

Puede usar el evento OnKeyDown para capturar las teclas presionadas y suprimir las que no desea permitir.

BabySmash application de Scott Hanselman desactiva la mayoría de los trazos de tecla, como alt-tab alt-esc, etc. La mayoría de los source and development se pueden encontrar en su blog. La fuente está en GitHub. En la fuente, verá la clase InterceptKeys que usa muchas llamadas win32 para obtener ganchos de bajo nivel para las teclas presionadas. A continuación, maneja estos en HookCallback en el archivo App.xaml.cs. Espero que esto ayude.

Similar Question

Another Similar

+0

agregó otra pregunta SO similar. No se puede capturar ctrl-alt-del, y probablemente no alt-tab, pero la mayoría de los demás. Debería poder hacer la tecla de ganar. –

+0

el ejemplo babysmash hace lo que está buscando –

+2

Alt-Tab es atrapable, pero no se recomienda. Ctrl-Alt-Del no se puede atrapar en el modo de usuario. Punto final. – ChrisV

Cuestiones relacionadas