最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect Fn key with js - Stack Overflow

programmeradmin3浏览0评论

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

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?

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

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?

Share Improve this question edited Apr 20, 2018 at 20:59 Ananta asked Apr 20, 2018 at 16:11 AnantaAnanta 7001 gold badge7 silver badges19 bronze badges 1
  • linked to this, the logical answer was to use keyup to detect the combination of keys... – Uneconscience UneSource Commented Mar 22, 2024 at 9:42
Add a comment  | 

5 Answers 5

Reset to default 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/

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

Try this,

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

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

Thanks.

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");
}

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

发布评论

评论列表(0)

  1. 暂无评论