1

I have a c# winforms app that can be active or minimized. I want to be able to capture and override any existing shortcut so that NO matter what my app will respond to "Alt+D".

If the app has focus I can do this with

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == (Keys.Alt | Keys.D))
        {

            return true;    
        }


        return base.ProcessCmdKey(ref msg, keyData);
    }

But when the app doesnt have focus it doesnt execute, e.g. if I am inside firefox my Ctrl D keystroke sequence doesnt get activated

How can I get this to work?

Ideally I want to be able to highlight with the mouse / select some text in ANY app and press Alt D and then send this text to my app.

update

http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/

This worked for me perfectly

user1320651
  • 808
  • 2
  • 15
  • 42
  • I'm looking at [this answer](http://stackoverflow.com/a/5951527/1043380) and it looks to be the same as what you have, but it is marked as correct. – gunr2171 Aug 21 '13 at 14:57
  • http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/ – user1320651 Aug 21 '13 at 15:44

3 Answers3

1

Keyboard events are generally sent to the application which is currently in focus. In order to handle keyboard events when your application is not in focus you generally need to setup a keyboard hook

A hook essentially allows a routine to intercept events before they are passed to the target application.

However I strongly recommend against attempting to do this. An application which handles keyboard events sent to other applications is highly annoying. It prevents me from using other applications in the way I expect to use them. Generally I respond by uninstalling the app which is stealing my keystrokes

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • OK, Click.to is similar, it pops up with a configurable menu so it allows the default or configured option, I jsut dont want to use Click.to. If I could define something REALLY unique maybe double mouse click (left and right button at the same time) I could get this to work – user1320651 Aug 21 '13 at 15:02
  • @user1320651 unique needs no modifier, it is singular. The fact that you are trying to attribute a modifier to unique is a sign that it is in fact not unique. There is no combination you can choose that is a) quick and b) not used by another application. It will conflict and annoy users IMHO – JaredPar Aug 21 '13 at 15:04
  • http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/ very good overview of another solution. Very simple. – user1320651 Aug 21 '13 at 15:44
0

You can use another service to detect keypress all the time and communicate it with your app. Here is a similiar question: Monitoring for specific Keystrokes in C#

Community
  • 1
  • 1
Kuzgun
  • 4,649
  • 4
  • 34
  • 48
0

What are you looking for seems to be a keyboard hook: http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

This class allows you to tap keyboard and mouse and/or to detect their activity even when an application runs in the background or does not have any user interface at all. This class raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need.

Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92