Is it Possible to invoke keyboard Function keys using Chrome Extensions

I am working on Chrome Extensions. I want to know that is it Possible to invoke keyboard Function keys using Chrome Extensions.

Thanks, NVN.

Answers


EDIT: Do you want to simulate function key presses on a page or listen for physical key presses from the user? If you want want to procedurally trigger function key actions, you can't. As Rob pointed out, scripted events only activate scripted event listeners, and do not trigger default behavior.

In order to detect function key presses, you need to bind your events to keyup -- not to keypress, which doesn't fire for several non-printable keystrokes, like function keys and arrow keys.

document.documentElement.addEventListener("keyup", function(e) {
    if(e.keyCode == 113) {
        // if F2 is pressed...
        // F1 is keycode 112, and it increments from there
    }
});

Some function keys do things already (F1 opens help, F5 refreshes, etc.). If you have a good reason for preventing this behavior (e.g. you're making an immersive full-screen app like a VNC viewer that shouldn't exhibit normal browser behavior), you can use preventDefault on keydown to stop that potentially disruptive action:

document.documentElement.addEventListener("keydown", function(e) {
    if(e.keyCode == 112) {
        // if F1 is pressed, don't open help
        e.preventDefault();
    }
});

To see how to handle key presses for your Chrome extension, see my answer on Activate extension via short cut key; that should be exactly what you need for the rest. Briefly:

  • the code I gave above goes in a content script that gets added to each page

  • the event handlers functions perform some action, either within the content script on the page or by passing a message to the background page


Need Your Help

Conflict with jQuery scripts

javascript jquery html conflict

I have these jQuery scripts in my HTML and they work very well...

webfont causing div to not adjusting for correct height

jquery scroll jscrollpane

I'm using webfont service and I'm having issues with jscrollpane.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.