Emulating mouse click with a keyboard

13

2

I want to make a button on a keyboard to act like a mouse click, preferably using xmodmap. How do I do it?

Running Ubuntu 10.10.

Zona

Posted 2011-09-17T09:35:45.203

Reputation: 131

setxkbmap -option keypad:pointerkeys would do what you want. Then a shift-numlock. – peterh - Reinstate Monica – 2019-01-22T12:04:09.817

Answers

6

The following steps would help you rewire your keyboard keys to mouse buttons.

  1. Identify the keyboard key using xev utility.

    Running xev utility and pressing the keyboard key to be rewired will output something like this

    KeyPress event, serial 37, synthetic NO, window 0x3e00001,
    root 0x82, subw 0x0, time 28977858, (-419,294), root:(175,613),
    state 0x0, keycode 38 (keysym 0x61, a), same_screen YES,
    XLookupString gives 1 bytes: (61) "a"
    XmbLookupString gives 1 bytes: (61) "a"
    XFilterEvent returns: False

    Note the keycode which will be reamapped.

  2. Run the xkbset utilty

    xkbset m

  3. Remap using xmodmap utility.

    In the below case menu key is remapped to right mouse button.

    xmodmap -e "keycode 135 = Pointer_Button3"

ramasamyz

Posted 2011-09-17T09:35:45.203

Reputation: 61

see my script https://superuser.com/questions/313926/use-keys-for-mouse-buttons-on-linux-alternative-to-ahk Use keys for mouse buttons on linux. Alternative to AHK?

– Russo – 2018-06-07T04:10:06.683

4

I imagine you could accomplish this using xmodmap and the Pointer_Button1 keymap.

Look in the file /usr/include/X11/keysymdef.h for the exact spelling and capitalization, but it should be possible to map a keyboard key into a mouse button (1,2,3,etc), even a double click (see the header file, near line 460 or so)

keysym W = Pointer_Button1

MIGHT work, causing the W key to act as the left mouse button. I'd recommend using a different key, unless you don't use any W's, but whatever works for you. (no, I'm not going to try it myself, I need my W's!)

Also have a look at the man page for xmodmap, and certainly the contents of /usr/include/X11/keysymdef.h to get the spelling correct. Drop the XK_ prefix to use the keysym names for xmodmap.

Looks like it's (keysymdef.h) in the x11-proto-core-dev package for my Debian system... you may want to search for the file and its contents online, doubt you want to install all the -dev files involved to just get THAT one. GIYF.

Something tickling in back of my head about another way to do this, but nothing's coming to me just now.

OH! Depending on your needs, you might look into Keyboard Mouse Emulation, with turns the numeric keypad into a full featured two-button (no scroll wheel!) mouse.

lornix

Posted 2011-09-17T09:35:45.203

Reputation: 9 633

1

For readers' info, about "Keyboard Mouse Emulation": http://en.linuxreviews.org/HOWTO_use_the_numeric_keyboard_keys_as_mouse_in_XOrg The key combination to turn this on has been disabled by default since a while ago and now needs Option XkbOptions "keypad:pointerkeys". Hitting keys is not the only way to turn this on: xkbset m does this as per http://crunchbang.org/forums/viewtopic.php?id=27937 , and can control the nasty timeout and also turn it off from the cmdline.

– imz -- Ivan Zakharyaschev – 2016-06-22T19:09:51.580

3

The built-in "Keyboard Mouse Emulation" mode can do this and/but has pre-configured key combinations for emulating various mouse clicks and presses (for dragging).

(FYI: The key combination to turn this on has been removed from the default keymap since a while ago and now needs Option XkbOptions "keypad:pointerkeys".)

Hitting a key is not the only way to turn this mode on:

xkbset m

does this as per http://crunchbang.org/forums/viewtopic.php?id=27937, and can control the nasty timeout and also turn it off from the cmdline. To turn off the nasty timeout:

xkbset exp =m

So, xkbset is really more handy (than having a key just for turning this mode on), because xkbset gives more control over the preferred timeout etc.

The numpad is used. Some basic combinations (more at http://en.linuxreviews.org/HOWTO_use_the_numeric_keyboard_keys_as_mouse_in_XOrg:

  • /, *, - are for selecting the left, middle or right button;
  • 5 is for clicking with the selected button;
  • 0 and . are for pressing and releasing the selected button (for dragging etc.)

Notes on problems getting "Keyboard Mouse Emulation" always enabled

  1. I couldn't get "Keyboard Mouse Emulation" always enabled. So I relied on a special key (like in keypad:pointerkeys) to do this.

  2. I've found out that Xfce was messing with these settings, too, on startup, so if xkbset m is run before Xfce, then the setting is not active. Therefore, I had to change a setting in Xfce configuration to match my overall wish, namely: Xfce Settings: Accessibility: Mouse: enable emulation. (The source for me learning about the existence of such Xfce option was: https://bugzilla.xfce.org/show_bug.cgi?id=3860.) This seems to have helped. (I'm also disabling my touchpad with xinput float ...; this is messed by switching between Linux consoles, but can be worked around again by disabling this device in Xfce settings at least.)

imz -- Ivan Zakharyaschev

Posted 2011-09-17T09:35:45.203

Reputation: 443