2

I am working on an application for a customer and have hit a problem.

The application talks to the mobile phone and does a bunch of call handling. One of the things it does is to show an "answer call" button. Clicking this with the mouse works fine.

But the customer wants to have a keyboard shortcut for this, and that's a problem. I can get the focus if a window in the application has focus. But Windows focus steal prevention doesn't allow me to take focus if the user is in a different application.

Please don't discuss the pros and cons of focus stealing here. I know them already and have given them to my customer. Wrong or not, they still want to do this, and they are paying the bill, so they get to decide.

There are a number of workarounds for this, but they do not seem to work anymore. For example, I set HKEY_CURRENT_USER\Control Panel\Desktop\ForegroundFlashCount to 3 and ...\ForegroundLockTimeout to 0.

So what are my options? Is this impossible? Or do I have to build a keyboard hook application that virus checkers will hate?

This is a Qt/C++ application, but if you have C# example code that can do this, that is great as well.

I hope you can help.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • You may check this solution: http://stackoverflow.com/questions/17403511/qwidget-on-mac-os-x-not-focusing-in-qt-5-x – Dmitry Sazonov Jun 20 '14 at 12:12
  • 3
    If you just need a specific shortcut, it's better to leave focus steal protection as it is and register a single system wide shortcut. Qt doesn't provide this ability, but you can use `QxtGlobalShortcut` or WinAPI based solutions. – Pavel Strakhov Jun 20 '14 at 13:32
  • Do you want to move the focus back to your application, or do you just want to catch the shortcut? And yes, it is an appalling idea, but if the customer insists, what can you do? – David Heffernan Jun 20 '14 at 14:44
  • Failing all else you can use SystemParametersInfo to set SPI_GETFOREGROUNDLOCKTIMEOUT to zero; this turns off focus steal protection altogether. Note that you must currently have the focus in order to change this setting. – Harry Johnston Jun 22 '14 at 01:09

1 Answers1

2

I do not know how dated this is but you could try RegisterHotKey.

It allows you listen for keyboard events system wide and not just when your application has focus. You don't have to provide your window handle, if you leave that argument null the events are still posted to your thread.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
deek0146
  • 962
  • 6
  • 20