Bob Rowland
Bob Rowland

Reputation: 21

Can a web application catch key presses when it doesn't have focus?

I'd like to develop a voice chat application that would run inside of a web browser. My main concern is implementing push-to-talk functionality when the site doesn't have focus.

Say, for example, that the user is playing a video game, and they decide to press and hold the "C" key to indicate that they'd like to talk. I want my web application to respond to that, even though it wouldn't have focus at the time. Is this possible (using any language)?

Upvotes: 2

Views: 253

Answers (2)

ClosureCowboy
ClosureCowboy

Reputation: 21541

It isn't necessarily impossible, but as of right now, it is.

Keyboard events target the currently-in-focus DOM element. Currently, all web browsers will remove focus from any DOM element when the browser is minimized, or when the user clicks outside of the page.

With the increased focus on web-app-support in modern browsers, this rule could theoretically change, but I haven't heard of any browser vendors considering it.

Upvotes: 1

Justin
Justin

Reputation: 86729

I can't see any way of doing this without installing some client side functionality (such as an ActiveX component or similar) - a browser won't normall allow JavaScript or similar code to handle keypresses when the window is not active as this would be a security vulnerability.

Upvotes: 2

Related Questions