Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Focus

Focus

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nc3b
    wrote on last edited by
    #1

    Hello.. i have a problem.. i have an application, and i want it to do smth when a key-combination is pressed... the problem is that i want it to do that even when it doesn't have focus.. i need smth like a "global hotkey", or i need to set up an event listener, that can listen for events even when my application doesn't have focus.. can anybody help? pls...

    M 1 Reply Last reply
    0
    • N nc3b

      Hello.. i have a problem.. i have an application, and i want it to do smth when a key-combination is pressed... the problem is that i want it to do that even when it doesn't have focus.. i need smth like a "global hotkey", or i need to set up an event listener, that can listen for events even when my application doesn't have focus.. can anybody help? pls...

      M Offline
      M Offline
      Mark Greenwood
      wrote on last edited by
      #2

      Ok in your app import the User32.dll function RegisterHotKey [DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code ); Whilst Your at it - may as well import the Unregister one too [DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); In your MainForm constructor - register the hot key RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L); In this case it's ctrl, shift and L - anything you want really. Then override the WndProc for the main form and handle the event protected override void WndProc(ref Message m) { // Const for the WM_HOTKEY message const int WM_HOTKEY = 0x0312; // Switch on the message switch(m.Msg) { case WM_HOTKEY: { // Process the hot key press here break; } } // Pass message onto base base.WndProc (ref m); } Remember to also unregister the hot key in the Form.OnClosed method private void Form1_Closed(object sender, System.EventArgs e) { // Unregister the hot key UnregisterHotKey(Handle, 100); } That should do what you want. Mark. :)

      N 2 Replies Last reply
      0
      • M Mark Greenwood

        Ok in your app import the User32.dll function RegisterHotKey [DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code ); Whilst Your at it - may as well import the Unregister one too [DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); In your MainForm constructor - register the hot key RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L); In this case it's ctrl, shift and L - anything you want really. Then override the WndProc for the main form and handle the event protected override void WndProc(ref Message m) { // Const for the WM_HOTKEY message const int WM_HOTKEY = 0x0312; // Switch on the message switch(m.Msg) { case WM_HOTKEY: { // Process the hot key press here break; } } // Pass message onto base base.WndProc (ref m); } Remember to also unregister the hot key in the Form.OnClosed method private void Form1_Closed(object sender, System.EventArgs e) { // Unregister the hot key UnregisterHotKey(Handle, 100); } That should do what you want. Mark. :)

        N Offline
        N Offline
        nc3b
        wrote on last edited by
        #3

        Hey :). Thanks a lot:)

        1 Reply Last reply
        0
        • M Mark Greenwood

          Ok in your app import the User32.dll function RegisterHotKey [DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code ); Whilst Your at it - may as well import the Unregister one too [DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); In your MainForm constructor - register the hot key RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L); In this case it's ctrl, shift and L - anything you want really. Then override the WndProc for the main form and handle the event protected override void WndProc(ref Message m) { // Const for the WM_HOTKEY message const int WM_HOTKEY = 0x0312; // Switch on the message switch(m.Msg) { case WM_HOTKEY: { // Process the hot key press here break; } } // Pass message onto base base.WndProc (ref m); } Remember to also unregister the hot key in the Form.OnClosed method private void Form1_Closed(object sender, System.EventArgs e) { // Unregister the hot key UnregisterHotKey(Handle, 100); } That should do what you want. Mark. :)

          N Offline
          N Offline
          nc3b
          wrote on last edited by
          #4

          still, i have a problem....:( "KeyModifiers fsModifiers" the program throws an error when i attempt to build it.. " The type or namespace name 'KeyModifiers' could not be found (are you missing a using directive or an assembly reference?)" it's a common error.. pls help, the rest of the code is perfect:)

          M 1 Reply Last reply
          0
          • N nc3b

            still, i have a problem....:( "KeyModifiers fsModifiers" the program throws an error when i attempt to build it.. " The type or namespace name 'KeyModifiers' could not be found (are you missing a using directive or an assembly reference?)" it's a common error.. pls help, the rest of the code is perfect:)

            M Offline
            M Offline
            Marc 0
            wrote on last edited by
            #5

            [Flags()]
            public enum KeyModifiers
            {
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8 //Not sure about the value 8...
            }

            Hope this helps you out! :cool:


            N 1 Reply Last reply
            0
            • M Marc 0

              [Flags()]
              public enum KeyModifiers
              {
              Alt = 1,
              Control = 2,
              Shift = 4,
              Windows = 8 //Not sure about the value 8...
              }

              Hope this helps you out! :cool:


              N Offline
              N Offline
              nc3b
              wrote on last edited by
              #6

              Hey, thx :)!

              M 1 Reply Last reply
              0
              • N nc3b

                Hey, thx :)!

                M Offline
                M Offline
                Marc 0
                wrote on last edited by
                #7

                You're welcome! :cool:


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups