2010-07-09 15 views
1

Je serai reconnaissant à tous ceux qui pourraient aider!commande SendKey C#

J'utilise la classe InterceptKeys ci-dessous pour changer quelques-unes des clés. Par exemple, lorsque je tape sur la touche support ouverte « [ », je veux changer cela « ë » (chaque fois que je tape dans mon ordinateur, et non pas un programme particulier). -je utiliser un SendKey.Send ("ë"); chaque fois que le « [ » est tapé, mais le problème est que, après « ë » alsot le « [ » est tapé. Est-il possible de remplacer le « [ » avec « ë » ou une touche, ou d'annuler le « [ » et juste envoyer « ë »?


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); 
      if (vkCode == (int)Keys.OemOpenBrackets)) 
      { 

       SendKeys.Send("ë");     

      } 
     } 

     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); 
} 

Répondre

1

Retour 1 si vous avez géré la clé au lieu d'appeler CallNextHookEx.

IE, HookCallback() devrait ressembler à ceci

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); 
     if (vkCode == (int)Keys.OemOpenBrackets)) 
     { 

      SendKeys.Send("ë");     
      return 1; 
     } 
    } 

    return CallNextHookEx(_hookID, nCode, wParam, lParam); 
} 
+0

Cela fonctionne parfaitement :) Je vous remercie beaucoup! – Coalen

+0

Content de pouvoir aider. Marquez ceci comme réponse et votez-le. :) –

+0

Malheureusement, je ne peux pas voter. Je reçois ce message quand j'essaie de: "Votez jusqu'à exige 15 réputation". En tout cas je l'ai marqué comme ma réponse acceptée. – Coalen