wilix-team / iohook

Node.js global keyboard and mouse listener.
https://wilix-team.github.io/iohook
MIT License
1.19k stars 291 forks source link

Keydown event only triggered by modifier key with Electron 12 #332

Open jopemachine opened 3 years ago

jopemachine commented 3 years ago

Expected Behavior

Keydown event is triggered by all type of key down

Current Behavior

Only detect modifier key (cmd, ctrl, alt...).

Alphabet, number keys are not detected.

Possible Solution

I have no idea why this is happening.

Steps to Reproduce (for bugs)

  1. Use iohook in Renderer process
  2. It is working fine, but only detect modifier key (cmd, ctrl, alt...). alphabet, number key is not detected.
  3. On other platforms, behavior is the same.

Context

ioHook.on('keydown', (e) => {
  console.log(e);
});

Your Environment

jopemachine commented 3 years ago

I don't know why this happens, the bug doesn't happen on production mode.

ash0x0 commented 3 years ago

@jopemachine it'll be some time before I get around to electron 12 and 13 but do provide more information if you can, such as if this behavior is the same on other OSes and what you're using to build your code for production.

jopemachine commented 3 years ago

@jopemachine it'll be some time before I get around to electron 12 and 13 but do provide more information if you can, such as if this behavior is the same on other OSes and what you're using to build your code for production.

Now I've made the project public.

here is my code using iohook.

if this behavior is the same on other OSes

Yes, this behavior is same on other OSes.

ash0x0 commented 3 years ago

@jopemachine thank you. It seems that for this particular use you need keydown event. If I'm mistaken about that then you might want to try keypress event instead. It will probably be around a week at least before I can fix this because there are other issues affecting this that need to be fixed first.

jopemachine commented 3 years ago

@jopemachine thank you. It seems that for this particular use you need keydown event. If I'm mistaken about that then you might want to try keypress event instead. It will probably be around a week at least before I can fix this because there are other issues affecting this that need to be fixed first.

I appreciate your hard work.

I tried to replaceing the event with the keypress event, but unfortunately keypress event was not triggered.

Here is my test code.


  useEffect(() => {
    ioHook.on('keypress', (e: IOHookKeyEvent) => {
      // not works on development mode, production mode both.
      console.log('triggered!');

      if (cpyKeyPressed(e)) {
        setTimeout(() => {
          const copiedText = clipboard.readText();
          if (copiedText !== '') {
            ipcRenderer.send(IPCRendererEnum.dispatchAction, {
              destWindow: 'clipboardHistoryWindow',
              actionType: actionTypes.PUSH_CLIPBOARD_STORE,
              args: JSON.stringify({
                text: clipboard.readText(),
                date: new Date().getTime(),
              }),
            });
          }
        }, 25);
      }

      if (isShiftKey(e)) {
        handleDoubleKeyModifier('shift');
      } else if (isAltKey(e)) {
        handleDoubleKeyModifier('alt');
      } else if (isCtrlKey(e)) {
        handleDoubleKeyModifier('ctrl');
      } else if (isMetaKey(e)) {
        handleDoubleKeyModifier('cmd');
      }
    });

    ioHook.start();

    return () => {
      ioHook.removeAllListeners();
      ioHook.unload();
    };
  }, []);
bugii commented 3 years ago

I think I also once encountered that issue on macOS. For me, it was related to the fact that the terminal application used to start my application no longer had the required accessibility privileges (for keyboard input). After enabling them again in the system settings, everything worked again.

jopemachine commented 3 years ago

I think I also once encountered that issue on macOS. For me, it was related to the fact that the terminal application used to start my application no longer had the required accessibility privileges (for keyboard input). After enabling them again in the system settings, everything worked again.

I appreciate for your comment :)

It works well on dev mode now after activating vscode's Input Monitoring permission on mac.

I think maybe there is another similar kind of permission setting in other OSs too.