Is it Possible to invoke keyboard Function keys using Chrome Extensions - google-chrome

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

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

Related

Triggering button event (or atleast appear to) via keyboard press AS3

Attempting to make a piano in AS3 and so far I have 2 methods of triggering a note, 1 via keyboard and 1 via mouse-click. When I click the key, it changes colour to indicate that the key has been pressed. Unfortunately , I can't find a way for the keyboard event to trigger the button press, is it possible and how would I do it?
It doesn't need to be an actual button press, it can instead just appear to be.
EDIT
The button is a button object, using the GotoAndStop causes the following error code:
"project.as, Line 13, Column 7 1061: Call to a possibly undefined method gotoAndStop through a reference with static type flash.display:SimpleButton."
Thanks in advance.
The easiest way is to extract the piano key press logic into its own function, and then call that function from two events.
function pressedMyPianoKey(){
// Do something like pianoKey.gotoAndStop(2) to show a new graphic
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
function keyDown(event:KeyboardEvent):void
{
pressedMyPianoKey();
}
pianoKey.addEventListener(MouseEvent.CLICK, click);
function click(e:MouseEvent){
pressedMyPianoKey();
}

WP8 : IExplorer 10 : on "keyup" event not firing for "RETURN" key

I've just came across this interesting problem, or probably it's just me missing something very basic (hopefully). The problem is, that the keyup event is not firing for the return key in the WP8 browser, whereas all the other standard keys are firing this event up.
I've tested this on the desktop, and IE10 is not having any issues with this, the event fires up nicely.
Can anyone else confirm that on their WP8 devices the keyup event is not being triggered for the "return" key ?
/using dojo syntax here/
on(dom.byId("testinput1"), "keyup", function(evt){
dom.byId("result1").innerHTML = "";
if (evt.keyCode == keys.ENTER)
dom.byId("result1").innerHTML = "ENTER pressed.";
});
Here is the jsfiddle link
open it in IE10 -> no problem for both of the input boxes
open it in WP8 browser (IE10) -> the first input box wont trigger keyup for the return key
Thanks for all your help.

AIR Keyboard shortcut for a button

I work with AIR application.
In this application, several windows are displayed.
I like to close windows with shortcuts like Esc and Enter.
I try to do that like this:
// On creation complete
this.addEventListener(KeyboardEvent.KEY_DOWN, exit_keyDownHandler);
// exit function
protected function exit_keyDownHandler(event:KeyboardEvent):void{
if ((event.keyCode == Keyboard.ENTER)
||(event.keyCode == Keyboard.NUMPAD_ENTER)
||(event.keyCode == Keyboard.ESCAPE)){
stage.nativeWindow.close();
}
}
But if a text area exists on window, a problem occured. Because when user presses Enter key text area window closes. How can that be avoided?
Closing with a single key other than the escape key doesn't seem like a very good idea to me, especially not with the enter key.
However, if you insist doing it like that, then you could try checking the current focused object to make sure it's not an input element/component that accepts the enter key.
See FocusManager.getFocus() and Stage.focus. A very restrictive check could for example look like this:
if(focusManager.getFocus() is IFocusManagerComponent ||
stage.focus is InteractiveObject || stage.focus is IFocusManagerComponent)
{
}

How to hook a keyboard event handler onto an INPUT element with jQuery?

I want to attach my own key event handler to an INPUT that already has another event handler attached to onkeydown. Essentially, I want to receive a key event before all the other handlers and check if the user pressed a certain key -- if yes, I want to perform some functions and discard the event, if no, I want to pass it along to the other handler(s).
How can I do this with jQuery?
If you are loading a 3rd party script or jQuery addon you can just load your script or function first. If you do that then you can use something like this without the mess of unbinding and rebinding event handlers.
// your possible interceptor code
$("#awesome").keydown(function(e) {
if (e.keyCode < 70) {
e.stopImmediatePropagation();
console.log("BLOCKED!!!");
};
});
// possible 3rd party event code loaded after your code
$("#awesome").keydown(function(e) {
console.log("3rd party:"+e.keyCode);
});
Example webpage => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-key-event-interception.html
Example output of Firebug console
jQuery stopImmediatePropagation() documentation
According to the jQuery bind() documentation:
"When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound."
So it looks like you will have to unbind the other handlers, bind yours and then add the others back if you want yours to run first.
There is some good information in this thread with respect to that:
jQuery: Unbind event handlers to bind them again later

How to detect if the delete key was pressed in Actionscript 3?

How do I determine if the delete key was pressed using actionscript?
addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
...
function onKeyUp(event:KeyboardEvent):void
{
trace(event.keyCode);
}
The above code yields no value when delete, backspace, enter, and other command keys are pressed. However, arrow keys do yield values.
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
....
function onKeyPressed(event:KeyboardEvent):void
{
if (event.keyCode==Keyboard.DELETE) {
.....
}
}
it's workin nice...
But if you test movie from Flash, it will not work, so export to swf and test....
Just guessing you are using the TEXT_INPUT event, this doesn't work for delete and backspace. To catch those ones you can add an eventListener on the stage and listen to a KeyboardEvent.
Code will work fine if the display object that you attached the listener is in focus. For global listening, as Theo said, you have to attach the listener to the stage. Accessing stage from an object that's not yet added to the display list will result in null error. Do it in the ADDED_TO_STAGE event handler to be safe.
Old thread, but if anyone gets this far: in the Flash Player inside the IDE, these keys are associated with shortcuts. When you test your movie, choose Control>disable keyboard shortcuts in the player and you'll get the events back.