Detect Fn key with js
Asked Answered
L

5

8

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?

Latimer answered 20/4, 2018 at 16:11 Comment(1)
linked to this, the logical answer was to use keyup to detect the combination of keys...Featurelength
B
8

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/

Biak answered 20/4, 2018 at 16:18 Comment(1)
There's this other alternative keyjs.dev that has a bit more information about the different KeyboardEvents and their properties. 🙂Gracious
Q
6

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

Quentinquercetin answered 20/4, 2018 at 16:20 Comment(0)
A
1

Try this,

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

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

Thanks.

Airy answered 20/4, 2018 at 16:31 Comment(0)
F
0

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')...

Featurelength answered 22/3 at 9:51 Comment(0)
L
-1

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");
}
Lactam answered 20/4, 2018 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.