iohook icon indicating copy to clipboard operation
iohook copied to clipboard

Keydown event only triggered by modifier key with Electron 12

Open jopemachine opened this issue 4 years ago • 7 comments
trafficstars

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

  • Version used: 0.9.1
  • Environment name and version (e.g. Chrome 39, node.js 5.4): Electron 12.0.1
  • Operating System and version (desktop or mobile): Desktop, macos Big sur 11.4
  • Link to your project: Using electron-react-boilerplate, https://github.com/jopemachine/arvis/blob/master/src/app/hooks/useIoHook.tsx

jopemachine avatar Jun 03 '21 12:06 jopemachine

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

jopemachine avatar Jun 11 '21 10:06 jopemachine

@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.

ash0x0 avatar Jun 11 '21 17:06 ash0x0

@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.

jopemachine avatar Jun 14 '21 11:06 jopemachine

@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.

ash0x0 avatar Jun 15 '21 01:06 ash0x0

@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();
    };
  }, []);

jopemachine avatar Jun 16 '21 05:06 jopemachine

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.

bugii avatar Sep 08 '21 18:09 bugii

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.

jopemachine avatar Sep 09 '21 07:09 jopemachine