Ananta
Ananta

Reputation: 700

Detect Fn key with js

Hello I was wondering if it is possible to catch the fn key with js. fn key

What i'm trying to achieve is detect the fn + f7 keypress.

Here is my code which is not triggered with the fn key :

document.addEventListener("keydown", onKeyDown, false);

function onKeyDown(e) {
  console.log(e);
}

Update : I found that detecting fn + f5 works on windows but not for my linux. Is there a package to install to add media keys for firefox?

Upvotes: 8

Views: 15826

Answers (5)

Use keyUp instead of keyDown in your listener. Because you are doing a combination of keys. And use the normal detection if (event.key === 'F2')...

Upvotes: -1

Ashish
Ashish

Reputation: 19

To find which key is pressed:

var intKey=self.setInterval(onkeydown="findKey(event);",5000);
function findKey(e)
{
  console.log("keyCode for the key pressed: " + e.keyCode + "\n");
}

Upvotes: -1

Zied Chouk
Zied Chouk

Reputation: 21

Try this,

document.addEventListener("keydown", onKeyDown, false);

function onKeyDown(e) {
 var x = e.keyCode;
 if(x==118){
  console.log('Your pressed Fn+F7');
 }
}

Thanks.

Upvotes: 1

GSazheniuk
GSazheniuk

Reputation: 1384

You can't catch when fn key was pressed.

However if you press fn+F7 it would generate different event object than if you press solely F7 - considering that there is function bound to that key. So in my case I do not have anything bound on F7, therefore there will be no event generated if I press fn+F7 keys.

If I press F3 and then fn+F3, following codes will be generated:

KeyboardEvent {isTrusted: true, key: "F3", code: "F3", location: 0, ctrlKey: false, …}
KeyboardEvent {isTrusted: true, key: "AudioVolumeUp", code: "AudioVolumeUp", location: 0, ctrlKey: false, …}

Hope this helps.

----More info below----

As I expected fn key does not really generate any keycode. Instead, on the hardware level, when pressed in combination with some other keys it is generating unique keycode.

Information based on answer from this question: https://askubuntu.com/questions/827925/remapping-the-fn-key

And this: https://askubuntu.com/questions/270416/how-do-fn-keys-work

Upvotes: 6

leo
leo

Reputation: 276

Most FN keys are implemented in firmware and won't be recognized by applications. You can use a website like this one to test out what javascript can handle:

http://keycode.info/

Upvotes: 8

Related Questions