locked
Application Switching with a Global Hot Key RRS feed

  • Question

  • I'm working on writing an application that straddles the line between C# and C/++ on Windows Mobile 6.1/6.5. We currently have a kiosk application running on our devices, and would like to add the ability to switch back and forth to a second kiosk application.

    Our goal is to establish a global hot key that switches process windows (similar to the way that alt+tab works) whenever it is pressed. We already have both applications and I've written some code that switches the processes, but am having a rough time getting the global hot key portion of the project working.

    From all of the reading that I've done, my understanding is that the best way to monitor global key presses is to link into the system message pump with the SetWindowsHookEx function in coredll.dll. Unfortunately, I've also read that this function isn't technically supported on the platform. 

    I also found some tutorials that suggested using a message map with the ON_WM_KEYUP/ON_WM_KEYDOWN macros in the MFC framework, but couldn't find any documentation specific to Windows Mobile. When I tried to use the documentation here, my device kept crashing.

    Is there an accepted best practice for setting some kind of global key hook on the platform? If not, is there something that's at least technically supported?

    Thanks in advance.

    Monday, May 9, 2011 6:55 PM

Answers

  • Hello,

     

    I just found some codes about hook in Windows mobile , and I hope these codes can help you.

     

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    
    using System.Runtime.InteropServices;
    
    namespace SmartDeviceProject1
    {
      public class Hook
      {
        public delegate int HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
        private HookKeyProc hookKeyDeleg;
        public Hook()
        { }
        //install hook
        public void Start()
        {
          if (hHookKey != 0)
          {
            //Unhook the previouse one
            this.Stop();
          }
          hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
          hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
          if (hHookKey == 0)
          {
            throw new SystemException("Failed acquiring of the hook.");
          }
        }
    
    
    
      public void Stop()
        {
          UnhookWindowsHookEx(hHookKey);
        }
        private int HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
        {
          KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
          if (code < 0)
            return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
          if (hookStruct.vkCode == 0x5B)
          {
            //deal with some key
            return -1;       }
    
          if (hookStruct.vkCode == 0x25)
          {
             return -1;  
          }
          return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
        }
    
    
        [DllImport("coredll.dll")]
        private static extern int SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);
        //private static extern int SetWindowsHookEx(int type, HookMouseProc HookMouseProc, IntPtr hInstance, int m);
        [DllImport("coredll.dll")]
        private static extern IntPtr GetModuleHandle(string mod);
        [DllImport("coredll.dll")]
        private static extern int CallNextHookEx(HookKeyProc hhk,int nCode, IntPtr wParam,IntPtr lParam);
        [DllImport("coredll.dll")]
        private static extern int GetCurrentThreadId();
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int UnhookWindowsHookEx(int idHook);
    
        private struct KBDLLHOOKSTRUCT
        {
          public int vkCode;
          public int scanCode;
          public int flags;
          public int time;
          public IntPtr dwExtraInfo;
        }
    
        const int WH_KEYBOARD_LL = 20;
    
        public class KeyBoardInfo
        {
          public int vkCode;
          public int scanCode;
          public int flags;
          public int time;
        }
    
      }
      
    }
    
    

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Tuesday, May 17, 2011 7:55 AM
    • Marked as answer by Jesse Jiang Monday, May 23, 2011 12:43 PM
    Wednesday, May 11, 2011 3:05 AM

All replies

  • Hello,

     

    I just found some codes about hook in Windows mobile , and I hope these codes can help you.

     

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;
    
    using System.Runtime.InteropServices;
    
    namespace SmartDeviceProject1
    {
      public class Hook
      {
        public delegate int HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
        private HookKeyProc hookKeyDeleg;
        public Hook()
        { }
        //install hook
        public void Start()
        {
          if (hHookKey != 0)
          {
            //Unhook the previouse one
            this.Stop();
          }
          hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
          hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
          if (hHookKey == 0)
          {
            throw new SystemException("Failed acquiring of the hook.");
          }
        }
    
    
    
      public void Stop()
        {
          UnhookWindowsHookEx(hHookKey);
        }
        private int HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
        {
          KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
          if (code < 0)
            return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
          if (hookStruct.vkCode == 0x5B)
          {
            //deal with some key
            return -1;       }
    
          if (hookStruct.vkCode == 0x25)
          {
             return -1;  
          }
          return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
        }
    
    
        [DllImport("coredll.dll")]
        private static extern int SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);
        //private static extern int SetWindowsHookEx(int type, HookMouseProc HookMouseProc, IntPtr hInstance, int m);
        [DllImport("coredll.dll")]
        private static extern IntPtr GetModuleHandle(string mod);
        [DllImport("coredll.dll")]
        private static extern int CallNextHookEx(HookKeyProc hhk,int nCode, IntPtr wParam,IntPtr lParam);
        [DllImport("coredll.dll")]
        private static extern int GetCurrentThreadId();
        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int UnhookWindowsHookEx(int idHook);
    
        private struct KBDLLHOOKSTRUCT
        {
          public int vkCode;
          public int scanCode;
          public int flags;
          public int time;
          public IntPtr dwExtraInfo;
        }
    
        const int WH_KEYBOARD_LL = 20;
    
        public class KeyBoardInfo
        {
          public int vkCode;
          public int scanCode;
          public int flags;
          public int time;
        }
    
      }
      
    }
    
    

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Tuesday, May 17, 2011 7:55 AM
    • Marked as answer by Jesse Jiang Monday, May 23, 2011 12:43 PM
    Wednesday, May 11, 2011 3:05 AM
  • this code is missing detect the pulsation of the Volume keys and off to Leo HTC devices

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Text;

    using System.Runtime.InteropServices;

    namespace SmartDeviceProject1
    {
      publicclass Hook
      {
        publicdelegateint HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
        private HookKeyProc hookKeyDeleg;
        public Hook()
        { }
        //install hook
        publicvoid Start()
        {
          if (hHookKey != 0)
          {
            //Unhook the previouse one
            this.Stop();
          }
          hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
          hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
          if (hHookKey == 0)
          {
            thrownew SystemException("Failed acquiring of the hook.");
          }
        }



      publicvoid Stop()
        {
          UnhookWindowsHookEx(hHookKey);
        }
        privateint HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
        {
          KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
          if (code < 0)
            return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
          if (hookStruct.vkCode == 0x5B)
          {
            //deal with some key
            return -1;       }

          if (hookStruct.vkCode == 0x25)
          {
             return -1; 
          }
          return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
        }


        [DllImport("coredll.dll")]
        privatestaticexternint SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);
        //private static extern int SetWindowsHookEx(int type, HookMouseProc HookMouseProc, IntPtr hInstance, int m);
        [DllImport("coredll.dll")]
        privatestaticextern IntPtr GetModuleHandle(string mod);
        [DllImport("coredll.dll")]
        privatestaticexternint CallNextHookEx(HookKeyProc hhk,int nCode, IntPtr wParam,IntPtr lParam);
        [DllImport("coredll.dll")]
        privatestaticexternint GetCurrentThreadId();
        [DllImport("coredll.dll", SetLastError = true)]
        privatestaticexternint UnhookWindowsHookEx(int idHook);

        privatestruct KBDLLHOOKSTRUCT
        {
          publicint vkCode;
          publicint scanCode;
          publicint flags;
          publicint time;
          public IntPtr dwExtraInfo;
        }

        constint WH_KEYBOARD_LL = 20;

        publicclass KeyBoardInfo
        {
          publicint vkCode;
          publicint scanCode;
          publicint flags;
          publicint time;
        }

      }
     
    }

                                   
    Wednesday, March 7, 2012 3:47 PM