Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

masquerade_as option to allow output device to resemble input #42

Open
zw963 opened this issue Jun 17, 2022 · 28 comments
Open

masquerade_as option to allow output device to resemble input #42

zw963 opened this issue Jun 17, 2022 · 28 comments
Labels
enhancement New feature or request good first issue Good for newcomers help welcome Help/contrib is esp welcome

Comments

@zw963
Copy link

zw963 commented Jun 17, 2022

Sometimes the device details matter - like when libinput/Gnome is deciding if a keyboard/trackpad are "paired" for it's "ignore trackpad when typing" option... the virtual keyboard we present breaks this since it doesn't look like an "internal" paired keyboard... this can be fixed if we "masquerade" as the internal keyboard by copying all it's details and making our virtual keyboard look as similar as possible.

It seems reasonable to allow someone to do this in the config file. Proposed:

masquerade_as "AT Translated Set 2 keyboard"

The name would be the name of the device in --list-devices.


Original Title: Not respect GNOME touchpad disable-while-typing settings

This issue come from original xkeysnail discuss.

mooz/xkeysnail#144 (comment)

Maybe there exist some improvement, let us discuss here.

Thank you.

@joshgoebel joshgoebel changed the title Not respect GNOME touchpad disable-while-typing settings Consider: masquerade_as option to allow output device to resemble input Jun 17, 2022
@joshgoebel joshgoebel added enhancement New feature or request help welcome Help/contrib is esp welcome labels Jun 17, 2022
@joshgoebel joshgoebel changed the title Consider: masquerade_as option to allow output device to resemble input masquerade_as option to allow output device to resemble input Jun 17, 2022
@joshgoebel
Copy link
Owner

joshgoebel commented Jun 17, 2022

Until this is officially supported I think one could hack this in their config by redefining Output's _uinput themselves. Roughly:

from keyszer import output

# close the existing device
output._uinput.close() 

# create a new device with the exact options you want to mimic
output._uinput = UInput(...) 

@joshgoebel
Copy link
Owner

@zw963 If you switch to keyszer you should give that a try and report back if it works or not.

@joshgoebel joshgoebel added the good first issue Good for newcomers label Jun 17, 2022
@zw963
Copy link
Author

zw963 commented Jun 19, 2022

i try to this package get running on my laptop now, but it seem not working, no key can input, even, when i pressing Ctrl+C, could not stop it.

 ╰─ $ /home/zw963/utils/xkeysnail/bin/keyszer --watch -c /home/zw963/utils/xkeysnail/config.py
keyszer v0.5.0
(--) WATCH: Watching for new devices to hot-plug.
(--) Ready to process input.
(+K) Grabbing AT Translated Set 2 keyboard (/dev/input/event3)
(+K) Grabbing MOSART Semi. wireless dongle (/dev/input/event4)
(+K) Grabbing mykeyboarddevice (/dev/input/event28)
(+K) Grabbing mykeyboarddevice (/dev/input/event27)
Exception in callback receive_input(InputDevice('...nput/event27')) at /home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/input.py:189
handle: <Handle receive_input(InputDevice('...nput/event27')) at /home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/input.py:189>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/input.py", line 202, in receive_input
    on_event(event, device.name)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/transform.py", line 315, in on_event
    on_key(ks, context)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/transform.py", line 347, in on_key
    update_pressed_states(keystate)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/transform.py", line 87, in update_pressed_states
    del _key_states[keystate.inkey]
KeyError: <Key.LEFT_CTRL: 29>
Exception in callback receive_input(InputDevice('...nput/event27')) at /home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/input.py:189
handle: <Handle receive_input(InputDevice('...nput/event27')) at /home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/input.py:189>
Traceback (most recent call last):
  File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/input.py", line 202, in receive_input
    on_event(event, device.name)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/transform.py", line 315, in on_event
    on_key(ks, context)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/transform.py", line 347, in on_key
    update_pressed_states(keystate)
  File "/home/zw963/utils/xkeysnail/site-packages/keyszer/src/keyszer/transform.py", line 87, in update_pressed_states
    del _key_states[keystate.inkey]
KeyError: <Key.LEFT_CTRL: 29>

Following is code i add to my config:

output._uinput.close()
_keyboard_codes = ecodes.keys.keys() - ecodes.BTN 
output._uinput =  UInput(events={ecodes.EV_KEY: _keyboard_codes,
                         ecodes.EV_REL: set([0,1,6,8,9]),
                         },
                 name="mykeyboarddevice",
                 phys="isa0060/serio0/input0",
                 vendor=1,
                 product=1,
                 version=43841,
                 bustype=17,
                 input_props=[])

Following is the ouput of --list-devices

 ╰─ $ 1  keyszer --list-devices
keyszer v0.5.0
-------------------------------------------------------------------------------------
Device               Name                                Phys
-------------------------------------------------------------------------------------
/dev/input/event0    Sleep Button                        PNP0C0E/button/input0
/dev/input/event1    Lid Switch                          PNP0C0D/button/input0
/dev/input/event2    Power Button                        LNXPWRBN/button/input0
/dev/input/event3    AT Translated Set 2 keyboard        isa0060/serio0/input0
/dev/input/event4    MOSART Semi. wireless dongle        usb-0000:00:14.0-6.1/input0
/dev/input/event5    MOSART Semi. wireless dongle Mouse  usb-0000:00:14.0-6.1/input1
/dev/input/event6    MOSART Semi. wireless dongle Consumer Control
                                                         usb-0000:00:14.0-6.1/input1
/dev/input/event7    MOSART Semi. wireless dongle System Control
                                                         usb-0000:00:14.0-6.1/input1
/dev/input/event8    MOSART Semi. wireless dongle        usb-0000:00:14.0-6.1/input1
/dev/input/event9    MOSART Semi. wireless dongle        usb-0000:00:14.0-6.1/input1
/dev/input/event10   Wireless hotkeys                    hpq6001/input0
/dev/input/event11   PC Speaker                          isa0061/input0
/dev/input/event12   ST LIS3LV02DL Accelerometer         lis3lv02d/input0
/dev/input/event13   HP HD Webcam: HP HD Webcam          usb-0000:00:14.0-7/button
/dev/input/event14   HP WMI hotkeys                      wmi/input0
/dev/input/event15   HDA NVidia HDMI/DP,pcm=3            ALSA
/dev/input/event16   HDA NVidia HDMI/DP,pcm=7            ALSA
/dev/input/event17   HDA NVidia HDMI/DP,pcm=8            ALSA
/dev/input/event18   HDA NVidia HDMI/DP,pcm=9            ALSA
/dev/input/event19   HDA NVidia HDMI/DP,pcm=10           ALSA
/dev/input/event20   HDA NVidia HDMI/DP,pcm=11           ALSA
/dev/input/event21   HDA Intel PCH Mic                   ALSA
/dev/input/event22   HDA Intel PCH Headphone             ALSA
/dev/input/event23   PS/2 Generic Mouse                  isa0060/serio2/input0
/dev/input/event24   SynPS/2 Synaptics TouchPad          isa0060/serio3/input0
/dev/input/event25   Video Bus                           LNXVIDEO/video/input0
/dev/input/event26   Video Bus                           LNXVIDEO/video/input0

Following is my config:

 ╰─ $ python show_event_info.py 
device.phys:  isa0060/serio0/input0
device.info.vendor:  1
device.info.product:  1
device.info.version:  43841
device.info.bustype:  17
device.input_props():  []

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 19, 2022

(+K) Grabbing AT Translated Set 2 keyboard (/dev/input/event3)
(+K) Grabbing MOSART Semi. wireless dongle (/dev/input/event4)
(+K) Grabbing mykeyboarddevice (/dev/input/event28)
(+K) Grabbing mykeyboarddevice (/dev/input/event27)

Your double grabbing... if the name of the device doesn't matter you should use the same suffix we use to prevent this: Keyszer VIRTUAL.

IE:

mykeyboarddevice Keyszer VIRTUAL

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

Sorry, it still not working.

╰─ $ /home/zw963/utils/xkeysnail/bin/keyszer --watch -c /home/zw963/utils/xkeysnail/config_new.py
keyszer v0.5.0
(--) WATCH: Watching for new devices to hot-plug.
(--) Ready to process input.
(+K) Grabbing AT Translated Set 2 keyboard (/dev/input/event3)
(+K) Grabbing MOSART Semi. wireless dongle (/dev/input/event4)
(+K) Grabbing mykeyboarddevice (/dev/input/event28)

e.g. following mapping in config, it works on xkeysnail, but not keyszer.

define_modmap({
  Key.CAPSLOCK: Key.LEFT_CTRL,
  Key.RIGHT_ALT: Key.CAPSLOCK
})

@joshgoebel
Copy link
Owner

Please report other issues in a separate issue. This is only for discussion of masquerade.

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 21, 2022

Grabbing mykeyboarddevice (/dev/input/event28)

You still haven't added the suffix/prefix...

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

Grabbing mykeyboarddevice (/dev/input/event28)

You still haven't added the suffix/prefix...

Don't under well, what you means is this?

image

above config still not work, i have to close terminal use mouse to exit.

@joshgoebel
Copy link
Owner

The string must be exact and match the one in the source:

VIRT_DEVICE_PREFIX = "Keyszer VIRTUAL"

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

VIRT_DEVICE_PREFIX = "Keyszer VIRTUAL"

Sorry, i am really don't understood what you stand for. i am quite idiot in linux keyboard device, uinput ... those things.

Could you please give me a full config sample? if any place need changes in source code, please point it out too.

i thought i was sent all my device info.

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 21, 2022

It would be easier if you copied actual code snippets instead of screen shots which I cannot edit to correct... Refering to: #42 (comment)

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

It would be easier if you copied actual code snippets instead of screen shots which I cannot edit to correct... Refering to: #42 (comment)

import re
from xkeysnail.transform import *
from evdev import ecodes
from keyszer import output
from evdev.uinput import UInput

# close the existing device
output._uinput.close()

# device.phys:  isa0060/serio0/input0
# device.info.vendor:  1
# device.info.product:  1
# device.info.version:  43841
# device.info.bustype:  17
# device.input_props():  []

_keyboard_codes = ecodes.keys.keys() - ecodes.BTN

# create a new device with the exact options you want to mimic
output._uinput =  UInput(events={ecodes.EV_KEY: _keyboard_codes,
                         ecodes.EV_REL: set([0,1,6,8,9]),
                         },
                 name="mykeyboarddeviceKeyszerVIRTUAL",
                 phys="isa0060/serio0/input0",
                 vendor=1,
                 product=1,
                 version=43841,
                 bustype=17,
                 input_props=[])

# this map capslock to left ctrl, then, map right alt to capslock
# but it not work in keyszer.
define_modmap({
  Key.CAPSLOCK: Key.LEFT_CTRL,
  Key.RIGHT_ALT: Key.CAPSLOCK
})

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 21, 2022

With the correct spacing:

                         },
                 name="Keyszer VIRTUAL mykeyboarddevice",
                 phys="isa0060/serio0/input0",

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

╰─ $ /home/zw963/utils/xkeysnail/bin/keyszer --watch -c /home/zw963/utils/xkeysnail/config_new.py -vvvv
keyszer v0.5.0
(DD) CONFIG: /home/zw963/utils/xkeysnail/config_new.py
(--) WATCH: Watching for new devices to hot-plug.

(II) in LEFT_SHIFT (press)
(DD) on_key LEFT_SHIFT press
(DD) suspending keys [LShift<Key.LEFT_SHIFT>]

(II) in LEFT_SHIFT (release)
(DD) on_key LEFT_SHIFT release
(DD) resume because of mod release
(DD) resuming keys: [<Key.LEFT_SHIFT: 42>]
(OO) press LEFT_SHIFT
(OO) release LEFT_SHIFT
(--) Ready to process input.
(+K) Grabbing AT Translated Set 2 keyboard (/dev/input/event3)
(+K) Grabbing MOSART Semi. wireless dongle (/dev/input/event4)
(+K) Grabbing mykeyboarddevice (/dev/input/event28)

(II) in LEFT_CTRL (press)
(DD) on_key LEFT_CTRL press
(DD) suspending keys [LCtrl<Key.LEFT_CTRL>]

(II) in LEFT_CTRL (repeat)
(DD) on_key LEFT_CTRL repeat

(II) in LEFT_CTRL (repeat)
(DD) on_key LEFT_CTRL repeat

(II) in F (press)
(DD) on_key F press
(DD) resuming keys: [<Key.LEFT_CTRL: 29>]
(OO) press LEFT_CTRL
(OO) press F

(II) in F (release)
(DD) on_key F release
(OO) release F

(II) in LEFT_CTRL (release)
(DD) on_key LEFT_CTRL release
(DD) resume because of mod release
(OO) release LEFT_CTRL

Still not work for my config.

it should map C-f to C-right in my firefox, but not work. (above log is when i pressing Ctrl+f)

following is my config:

define_keymap(lambda wm_class: wm_class not in ("Emacs" "konsole" "alacritty" "kitty"  "Hyper" "Bcompare4"), {
    K("M-f"): with_mark(K("C-right")),
})

@joshgoebel
Copy link
Owner

(+K) Grabbing mykeyboarddevice (/dev/input/event28)

The fact that it keeps saying this means you have NOT modified your config properly to add the prefix even if you think you have...

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

(+K) Grabbing mykeyboarddevice (/dev/input/event28)

The fact that it keeps saying this means you have NOT modified your config properly to add the prefix even if you think you have...

Wired, but, the code in /home/zw963/utils/xkeysnail/config_new.py really same as what i send to you.

for the purpose of evidence, please see following command:

 ╰─ $ cat /home/zw963/utils/xkeysnail/config_new.py |dpaste
http://dpaste.com/AGB6C6BR7

you can open http://dpaste.com/AGB6C6BR7 for check it.

@joshgoebel
Copy link
Owner

You're missing something because the name in your output isn't matching the name in your config:

(+K) Grabbing mykeyboarddevice (/dev/input/event28)

Maybe you have two config files or are using the wrong one, I dunno..

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

You're missing something because the name in your output isn't matching the name in your config:

(+K) Grabbing mykeyboarddevice (/dev/input/event28)

Maybe you have two config files or are using the wrong one, I dunno..

No. you saw my command.

╰─ $ /home/zw963/utils/xkeysnail/bin/keyszer --watch -c /home/zw963/utils/xkeysnail/config_new.py -vvvv
cat /home/zw963/utils/xkeysnail/config_new.py |dpaste

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 21, 2022

I dunno what to tell you... if the output has the old name (without the prefix), as you showed, then it's not getting updated somewhere.

(+K) Grabbing mykeyboarddevice (/dev/input/event28)

@zw963
Copy link
Author

zw963 commented Jun 21, 2022

so, that means the config code is wrong? what is the correct code to hack this?

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 21, 2022

I've given you the correct code #42 (comment)

If you say it's still not working i dunno what to tell you. 🙁 Maybe start with a fresh install/ fresh config/ double/triple/quadruple check everything...

@joshgoebel
Copy link
Owner

joshgoebel commented Jun 21, 2022

I just realized you could also use --device to list your devices manually as well:

# my setup:
keyszer --device /dev/input/event3 /dev/input/event2  

You'd have to use the correct device paths of course.

@zw963
Copy link
Author

zw963 commented Jun 23, 2022

keyszer --device /dev/input/event3 /dev/input/event2

 ╰─ $ 1  keyszer --help
usage: keyszer [-h] [-c config.py] [-d device [device ...]] [-w] [-v] [--list-devices] [--check] [--version]
               [--very-bad-idea]

A smart, flexible key remapper for Linux/X11.

options:
  -h, --help            show this help message and exit
  -c config.py, --config config.py
                        use custom configuration file
  -d device [device ...], --devices device [device ...]
                        manually specify devices to remap
  -w, --watch           watch for new hot-plugged devices
  -v                    increase debug logging
  --list-devices
  --check               evaluate config script, check for syntax errors
  --version
  --very-bad-idea       (deprecated: run as root)

@zw963
Copy link
Author

zw963 commented Jun 23, 2022

Okay, it can run now, thank you.

 ╰─ $ keyszer --device /dev/input/event3
keyszer v0.5.0
(--) Ready to process input.
(+K) Grabbing AT Translated Set 2 keyboard (/dev/input/event3)

What is the next step?

@joshgoebel
Copy link
Owner

What is the next step?

If you were running into other bugs or issues you should open an issue for them...

Repository owner deleted a comment from zw963 Jun 23, 2022
@joshgoebel
Copy link
Owner

joshgoebel commented Jun 23, 2022

Please open a separate issue as I've already asked you previously - this issue is ONLY for discussion of masquerade_as and workarounds... now that your keyboard itself is working and sending keys thru if you are having issue with key combos you need to open a separate issue - and include your configuration file as well.

@zw963
Copy link
Author

zw963 commented Aug 21, 2022

Miss the context again ...

How should i test on this? masquerade_as is still not merge into master, right?
because i even can't find this function out in the source code.

Following is my current config:

from evdev import ecodes
from keyszer import output
from evdev.uinput import UInput

output._uinput.close()
_keyboard_codes = ecodes.keys.keys() - ecodes.BTN
# create a new device with the exact options you want to mimic
output._uinput =  UInput(events={ecodes.EV_KEY: _keyboard_codes,
                                 ecodes.EV_REL: set([0,1,6,8,9]),
                                 },
                         name="Keyszer VIRTUAL mykeyboarddevice",
                         phys="isa0060/serio0/input0",
                         vendor=1,
                         product=1,
                         version=43841,
                         bustype=17,
                         input_props=[])

If i can replace above config with only one line masquerade_as ??? to serve same purpose?

Thank you.

@joshgoebel
Copy link
Owner

There is no PR for this yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help welcome Help/contrib is esp welcome
Projects
None yet
Development

No branches or pull requests

2 participants