4

I am trying to launch Xev from console, it always gives "unable to open display", I need to launch xev from a console as another user (I use sudo -u '#1000' to do this).

I know xev has a "display" argument (xev -display) but I just can't figure out how to use it.

How do I launch xev from the console from outside of my X11 environment?

Cestarian
  • 2,051
  • 1
    For all X11 programs there is an environment variable DISPLAY. For Display 0 you can do DISPLAY=:0 xev. Now you will get an error about security X11 has security to stop you doing this. You need to look into xauth. Note xhost + is dangerous. – ctrl-alt-delor Feb 19 '15 at 22:51

3 Answers3

4

All¹ X11 programs open their windows on the display indicated by the environment variable DISPLAY. Thus:

sudo -u 1000 env DISPLAY=:0 xev

or for that matter, since you can run programs as a different user from the X server, just

DISPLAY=:0 xev

:NUMBER is the notation for local displays; in most scenarios, the X11 server that is running on the console is the one that's started first and ends up being number 0. You can run echo "$DISPLAY" in a terminal on that display to check whether the display number is correct. :0.0 is equivalent to :0 (a trailing .0 can be omitted).

If you run the program as a different user from the X server, and sometimes even if you run it as the same user, you may need to set the XAUTHORITY environment variable as well. This variable points to a file that contains a password (called a cookie) that applications must pass to the X server. To see the right value from XAUTHORITY, run echo $XAUTHORITY on that display; if it's unset, the default value is ~/.Xauthority where ~ represents the user's home directory.

If you need to find the values of DISPLAY and XAUTHORITY programmatically, see Open a window on a remote X display (why "Cannot open display")?

¹ At least almost all. It's technically possible for them not to, but it takes active work on the part of the programmer to make it not so, whereas a -display argument is a convention that is far from universal.

1

You should try:

xev --display localhost:0.0

assuming that X is actually running.

Anthon
  • 79,293
-1

The following command is very useful:

xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

But you need to set DISPLAY variable and it's related to your position to xev machine.

NOTE: When you apply DISPALY variable that your position is illlegal, or you don't have any local access to machine.

PersianGulf
  • 10,850