locked
monitoring a shortcut key while application running in background RRS feed

  • Question

  • i've seen many applications that catches a shortcut key combination and respond to that in desired way?

    how can i implement this in my application?

    I want when my user select a text and presses a shortcut key then i can use that text to google that.

    please help
    Regards Kumar Gaurav.
    Saturday, November 5, 2011 2:06 PM

Answers

  • well i just gone through

    http://www.axino.net/tutorial/2009/10/keylogger-in-c-detecting-pressed-keys

    and implemented key detection, now how to get the selected text?


    Regards Kumar Gaurav.
    • Marked as answer by urprob Thursday, November 10, 2011 1:20 PM
    Thursday, November 10, 2011 10:42 AM

All replies

  • Take a look of this API

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990%28v=vs.85%29.aspx

    chanmm


    chanmm
    Sunday, November 6, 2011 2:17 PM
  • please read "Setting A Global Hot Key" article at CodeProject to set a global hot-key and monitor hot-key notifications.
    Any fool can know. The point is to understand.(Albert Einstein)
    • Proposed as answer by Malobukv Tuesday, November 8, 2011 5:08 PM
    Sunday, November 6, 2011 4:58 PM
  • > I want when my user select a text and presses a shortcut key then i can use that text to google that.


    try to use SetClipboardViewer function ...
    take a look at the Clipboard Chaining and the Registering/Unregistering for Activity Notifications.
    Sunday, November 6, 2011 5:28 PM
  • Hi urprob,

    You can simply override the Form's ProcessCmdKey as below:
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
        const int WM_KEYDOWN = 0x100; 
        const int WM_SYSKEYDOWN = 0x104; 
     
        if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN)) 
        { 
            switch (keyData) 
            { 
                case Keys.Down: 
                    this.Text = "Down Arrow Captured"; 
                    break; 
     
                case Keys.Up: 
                    this.Text = "Up Arrow Captured"; 
                    break; 
     
                case Keys.Tab: 
                    this.Text = "Tab Key Captured"; 
                    break; 
     
                case Keys.Control | Keys.M: 
                    this.Text = "<CTRL> + M Captured"; 
                    break; 
     
                case Keys.Alt | Keys.Z: 
                    this.Text = "<ALT> + Z Captured"; 
                    break; 
            } 
        } 
     
        return base.ProcessCmdKey(ref msg, keyData); 
    }
    
    And then write your logic in the case branch to perform some more operations.

    Have a nice day,
    Leo Liu [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.

    Monday, November 7, 2011 10:58 AM
  • Hi urprob,

    You can simply override the Form's ProcessCmdKey as below:
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
        const int WM_KEYDOWN = 0x100; 
        const int WM_SYSKEYDOWN = 0x104; 
     
        if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN)) 
        { 
            switch (keyData) 
            { 
                case Keys.Down: 
                    this.Text = "Down Arrow Captured"; 
                    break; 
     
                case Keys.Up: 
                    this.Text = "Up Arrow Captured"; 
                    break; 
     
                case Keys.Tab: 
                    this.Text = "Tab Key Captured"; 
                    break; 
     
                case Keys.Control | Keys.M: 
                    this.Text = "<CTRL> + M Captured"; 
                    break; 
     
                case Keys.Alt | Keys.Z: 
                    this.Text = "<ALT> + Z Captured"; 
                    break; 
            } 
        } 
     
        return base.ProcessCmdKey(ref msg, keyData); 
    }
    
    And then write your logic in the case branch to perform some more operations.

    Have a nice day,
    Leo Liu [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.


    It works but only when the application is running. i want this to work while application is running in background, for example when application is minimized

    Regards Kumar Gaurav.
    Tuesday, November 8, 2011 1:26 PM
  • > i want this to work while application is running in background


    in this case try using following
     

    KeyState.Wait(Key.CapsLock, state =>
    {
        System.Diagnostics.Trace.WriteLine(state, "changed");
    });


    this example captures changes in the state of CapsLock.
    the method implementation below.
     

    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Input;
    ...
    public static class KeyState
    {
        public static void Wait(Key key, Action<KeyStates> a)
        {
            var ao = AsyncOperationManager.CreateOperation(null);
            SendOrPostCallback cb = state => a((KeyStates)state);
            ThreadPool.QueueUserWorkItem(state =>
            {
                var vk = KeyInterop.VirtualKeyFromKey(key);  // WindowsBase.dll, v4.0.30319
                var prev = ((GetAsyncKeyState(vk) & 0x8000) == 0x8000);
                ao.Post(cb, prev ? KeyStates.Down : KeyStates.None);
                while (true)
                {
                    var res = ((GetAsyncKeyState(vk) & 0x8000) == 0x8000);
                    if (res != prev)
                        ao.Post(cb, res ? KeyStates.Down : KeyStates.None);
                    prev = res;
                }
            });
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        static extern short GetAsyncKeyState(int vkey);
    }
    

    • Proposed as answer by Malobukv Wednesday, November 9, 2011 6:48 AM
    Tuesday, November 8, 2011 6:39 PM
  • Hi urprob,

    How is it going now with Malobukv's suggestion?
    Have a nice day,
    Leo Liu [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.

    Thursday, November 10, 2011 3:00 AM
  • > i want this to work while application is running in background


    in this case try using following
     

    KeyState.Wait(Key.CapsLock, state =>
    {
        System.Diagnostics.Trace.WriteLine(state, "changed");
    });


    this example captures changes in the state of CapsLock.
    the method implementation below.
     

     

     

    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Input;
    ...
    public static class KeyState
    {
        public static void Wait(Key key, Action<KeyStates> a)
        {
            var ao = AsyncOperationManager.CreateOperation(null);
            SendOrPostCallback cb = state => a((KeyStates)state);
            ThreadPool.QueueUserWorkItem(state =>
            {
                var vk = KeyInterop.VirtualKeyFromKey(key);  // WindowsBase.dll, v4.0.30319
                var prev = ((GetAsyncKeyState(vk) & 0x8000) == 0x8000);
                ao.Post(cb, prev ? KeyStates.Down : KeyStates.None);
                while (true)
                {
                    var res = ((GetAsyncKeyState(vk) & 0x8000) == 0x8000);
                    if (res != prev)
                        ao.Post(cb, res ? KeyStates.Down : KeyStates.None);
                    prev = res;
                }
            });
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        static extern short GetAsyncKeyState(int vkey);
    }
    

     


    I tried but i stucked on KeyStates i had used windowsBase 3.0 dll.. is it only for 4.0???

    Regards Kumar Gaurav.
    Thursday, November 10, 2011 7:57 AM
  • > I tried but i stucked on KeyStates i had used windowsBase 3.0 dll.. is it only for 4.0???

     
    KeyStates (presentationcore.dll, versions: 3.0, ...)

    Thursday, November 10, 2011 8:08 AM
  • well i just gone through

    http://www.axino.net/tutorial/2009/10/keylogger-in-c-detecting-pressed-keys

    and implemented key detection, now how to get the selected text?


    Regards Kumar Gaurav.
    • Marked as answer by urprob Thursday, November 10, 2011 1:20 PM
    Thursday, November 10, 2011 10:42 AM
  • > well i just gone through http://www.axino.net/tutorial/2009/10/keylogger-in-c-detecting-pressed-keys and implemented key detection


    so you have chosen the solution suggested Ehsan above. don't forget to mark the helpful posts, please.

    > now how to get the selected text?

    it would be better if you'll create new thread with this question

    Thursday, November 10, 2011 11:12 AM
  • No sir, i got my solution from the link i typed. now as u said. i'll create new thread formy question. thanks
    Regards Kumar Gaurav.
    Thursday, November 10, 2011 1:22 PM