SitePoint Sponsor

User Tag List

Results 1 to 1 of 1

Thread: C# - Global hotkey doesn't work... please help

  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# - Global hotkey doesn't work... please help

    I need an activeX which handles global hotkey: Shift+b

    The code below is supposed to do this when I call this ActiveX from IE.

    I see the result of RegisterHotKey is true which means that the hotkey has been registered Ok.

    But I don't see that any messages come to the ThreadPreprocessMessage method.... why????

    Please help.

    Thank you





    Code Csharp:
    namespace Kosmala.Michal.ActiveXTest{
        public class ActiveXObject : NativeWindow, IDisposable {
            public const int WM_HOTKEY = 0x0312;
            private IntPtr pFoundWindow ;
     
            public ActiveXObject(){
                System.Windows.MessageBox.Show("constructor<<");
                Process[] processes = Process.GetProcessesByName("iexplore");
                foreach (Process p in processes){
                    pFoundWindow = p.MainWindowHandle;
                }
                System.Windows.MessageBox.Show("pFoundWindow:" + pFoundWindow);
                SetupHotKey(pFoundWindow);
                ComponentDispatcher.ThreadPreprocessMessage += ComponentDispatcher_ThreadPreprocessMessage;
                System.Windows.MessageBox.Show("constructor>>");
            }
     
            void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled){
                System.Windows.MessageBox.Show("inside handler");
                if (msg.message == WM_HOTKEY){
                    System.Windows.MessageBox.Show("inside handler");
                }
            }
     
            private void SetupHotKey(IntPtr handle){
                bool res = RegisterHotKey(handle, GetType().GetHashCode(), 0x0004, 0x42); //Shift + b
                System.Windows.MessageBox.Show("SetupHotKey res:"+res);
            }
     
            public void Dispose(){
                UnregisterHotKey(_host.Handle, GetType().GetHashCode());
            }
     
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
     
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    }
    Last edited by Mittineague; Nov 27, 2010 at 18:07. Reason: reformatting bbcode tags

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •