Linux/X11下如何隐藏鼠标指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/660613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you hide the mouse pointer under Linux/X11?
提问by rck
How do I hide the mouse pointer under X11? I would like to use the built in libraries in order to do this and not something like SDL (SDL_ShowCursor(0)) or glut (glutSetCursor(GLUT_CURSOR_NONE)). Also, the mouse pointer should be hidden no matter the pointer location, not just in its own window.
如何在X11下隐藏鼠标指针?我想使用内置库来做到这一点,而不是像 SDL (SDL_ShowCursor(0)) 或 glut (glutSetCursor(GLUT_CURSOR_NONE)) 这样的东西。此外,无论指针位置如何,鼠标指针都应该隐藏,而不仅仅是在其自己的窗口中。
采纳答案by ephemient
You can create and set an invisible cursor theme. This trick is used by maemo, because it's rather pointless to have a cursor on a touchscreen device.
您可以创建和设置不可见的光标主题。这个技巧被maemo 使用,因为在触摸屏设备上有一个光标是毫无意义的。
Sadly, the ability to change the global cursor theme at runtime is not uniform across X11 applications and toolkits. You can change the server resource Xcursor.theme
, and nobody will notice (generally it's only queried at startup); you can inform xsettingswhich only seems to affect Gtk+ programs; KDE uses some sort of communication through properties on the root window; etc.
遗憾的是,在运行时更改全局光标主题的能力在 X11 应用程序和工具包中并不统一。您可以更改服务器资源Xcursor.theme
,没有人会注意到(一般只在启动时查询);你可以通知xsettings似乎只影响 Gtk+ 程序;KDE 通过根窗口上的属性使用某种通信;等等。
At least changing the cursor for your own application is as easy as XDefineCursor, and if you do that on the root window, someapplications mightfollow along.
至少为您自己的应用程序更改光标就像XDefineCursor一样简单,如果您在根窗口上这样做,一些应用程序可能会跟随。
回答by rck
I ended up using XDefineCursor like ephemient mentioned. The control application changed the default root window cursor and the other applications (which are under my control) inherited it.
我最终使用了 XDefineCursor 就像提到的 ephemient 一样。控制应用程序更改了默认的根窗口光标,其他应用程序(在我的控制之下)继承了它。
Code specifics look like:
代码细节如下:
// Hide the cursor
if (NULL==(display=XOpenDisplay(NULL)))
{
printf("Unable to open NULL display\n");
exit(1);
}
window = DefaultRootWindow(display);
Cursor invisibleCursor;
Pixmap bitmapNoData;
XColor black;
static char noData[] = { 0,0,0,0,0,0,0,0 };
black.red = black.green = black.blue = 0;
bitmapNoData = XCreateBitmapFromData(display, window, noData, 8, 8);
invisibleCursor = XCreatePixmapCursor(display, bitmapNoData, bitmapNoData,
&black, &black, 0, 0);
XDefineCursor(display,window, invisibleCursor);
XFreeCursor(display, invisibleCursor);
XFreePixmap(display, bitmapNoData);
In order to hide the cursor and then after I'm done
为了隐藏光标然后在我完成后
// Restore the X left facing cursor
Cursor cursor;
cursor=XCreateFontCursor(display,XC_left_ptr);
XDefineCursor(display, window, cursor);
XFreeCursor(display, cursor);
To restore X's left handed cursor (Since it's the root window and I don't want it to stay invisible. I'm not sure, but I might also be able to use
恢复 X 的左手光标(因为它是根窗口,我不希望它保持不可见。我不确定,但我也可以使用
XUndefineCursor(display, window);
回答by Eugene Morozov
Here's a descriptionhow unclutter
utility does it.
这是实用程序如何做到这一点的描述unclutter
。
Unclutter is a program which runs permanently in the background of an X11 session. It checks on the X11 pointer (cursor) position every few seconds, and when it finds it has not moved (and no buttons are pressed on the mouse, and the cursor is not in the root window) it creates a small sub-window as a child of the window the cursor is in. The new window installs a cursor of size 1x1 but a mask of all 0, ie an invisible cursor. This allows you to see all the text in an xterm or xedit, for example. The human factors crowd would agree it should make things less distracting.
Once created, the program waits for the pointer to leave the window and then destroys it, restoring the original situation. Button events are passed transparently through to the parent window. They will usually cause the cursor to reappear because an active grab will be made by the program while the button is down, so the pointer will apparently leave the window, even though its x y position doesnt change.
Unclutter 是一个在 X11 会话后台永久运行的程序。它每隔几秒钟检查一次 X11 指针(光标)的位置,当它发现它没有移动(鼠标上没有按下任何按钮,并且光标不在根窗口中)时,它会创建一个小的子窗口作为光标所在窗口的子窗口。新窗口安装一个大小为 1x1 的光标,但掩码全为 0,即一个不可见的光标。例如,这允许您查看 xterm 或 xedit 中的所有文本。人为因素人群会同意它应该使事情不那么分散注意力。
一旦创建,程序等待指针离开窗口,然后销毁它,恢复原来的状态。按钮事件透明地传递到父窗口。它们通常会导致光标重新出现,因为程序会在按钮按下时进行主动抓取,因此即使其 xy 位置没有改变,指针显然也会离开窗口。
回答by sevenfourk
I'd rather use simpler method:
我宁愿使用更简单的方法:
unclutter -idle 0
You almost do not see cursor, still it is available. To disable mouse:
您几乎看不到光标,但它仍然可用。要禁用鼠标:
rmmod psmouse
Or disable mouse module permanently somewhere in /etc/. See your distribution manual.
或者在 /etc/ 中的某处永久禁用鼠标模块。请参阅您的分发手册。
回答by Emanuel Berg
This is my solution. It places the cursor where you can't see it (in my case, in the bottom-left corner) - then, it disables the mouse, so you can't move it.
这是我的解决方案。它将光标放在你看不到的地方(在我的例子中,在左下角) - 然后,它禁用鼠标,所以你不能移动它。
NoteYou could parse data from xrandr
, or put that data in an environmental on login, and then use it; that way, you won't have to have it hard coded. But, as for me, I never change my screen resolution, so 768 is OK :)
注意您可以从 解析数据xrandr
,或者在登录时将该数据放入环境中,然后使用它;这样,您就不必对其进行硬编码。但是,对于我来说,我从不改变我的屏幕分辨率,所以 768 是可以的 :)
setmouse () {
DISPLAY=":0" xinput `DISPLAY=":0" xinput | grep Mouse |
tr -d " " | tr "\t" " " |
cut -d" " -f2 | cut -d"=" -f2`
}
offmouse () {
DISPLAY=":0" xdotool mousemove 0 768 # use xrandr to find out
setmouse disable
}
onmouse () {
setmouse enable
}
回答by Aktau
an alternative to unclutter
整洁的替代品
Unclutterdidn't work for me, as it doesn't play well with hardware accelerated surfaces (such as for example those produced by intels' VA-API when decoding video). So I found a program that hid the mouse pointer in a less roundabout way, hhp, and rewrote it in C with minimal dependencies, the result is hhpc. I did this to obviate the need to have haskell to compile it and because hhp sometimes stopped hiding the mouse pointer.
Unclutter对我不起作用,因为它不适用于硬件加速表面(例如,在解码视频时由英特尔的 VA-API 产生的表面)。所以我找到了一个程序,它以一种不那么迂回的方式隐藏了鼠标指针hhp,并以最小的依赖关系用 C 重写了它,结果是hhpc。我这样做是为了避免需要 haskell 来编译它,因为 hhp 有时会停止隐藏鼠标指针。
hhpc, relies only on glibc
and xlib
, so it's easy to build, just do make release
. You can get the code and instructions from my repository. It's very memory and CPU efficient (because it does almost nothing).
hhpc,只依赖于glibc
andxlib
,所以很容易构建,只要 domake release
。您可以从我的存储库中获取代码和说明。它的内存和 CPU 效率很高(因为它几乎什么都不做)。
回答by StephaneAG
All right!
好的!
I guess this post may be getting a little bit old, but if what I've found can help some of us, I definitely have to post it here ;)
我想这篇文章可能有点老了,但如果我发现的内容可以帮助我们中的一些人,我肯定必须在这里发布;)
I found myself a clean and simple solution that works fine, without using "xcb" ( for what I tried to achieve, it was a little over-engineering (..)
我发现自己是一个干净简单的解决方案,可以正常工作,无需使用“xcb”(对于我试图实现的目标,它有点过度设计(..)
All you need is the xsetroot
command, with appropriate arguments:
您所需要的只是xsetroot
带有适当参数的命令:
To hide the mouse cursor, you need an extra little file (I called mine blnk_ptr.xbm
)
要隐藏鼠标光标,您需要一个额外的小文件(我称之为我的blnk_ptr.xbm
)
the content of this file:
该文件的内容:
#define blnk_ptr_width 1
#define blnk_ptr_height 1
#define blnk_ptr_x_hot 0
#define blnk_ptr_y_hot 0
static unsigned char blnk_ptr_bits[] = { 0x00 };
Then, we can use the two following commands:
然后,我们可以使用以下两个命令:
To hide the mouse pointer cursor,
$ xsetroot -cursor blnk_ptr.xbm blnk_ptr.xbm
To show the mouse pointer cursor again,
$ xsetroot -cursor_name left_ptr
(You can use a mouse pointer cursor other than "left_ptr", but this one seems to be widely available across *nix systems (..)
要隐藏鼠标指针光标,
$ xsetroot -cursor blnk_ptr.xbm blnk_ptr.xbm
要再次显示鼠标指针光标,
$ xsetroot -cursor_name left_ptr
(您可以使用“left_ptr”以外的鼠标指针光标,但这个光标似乎在 *nix 系统中广泛可用 (..)
Btw, I don't know yet how to get the name of the pointer currently used by the system using xsetroot. I guess I'll [as usual] digg that too, but I'd be happy to have someone who knows the how-to giving me the answer ( It'd be nice ;) )
顺便说一句,我还不知道如何使用 xsetroot 获取系统当前使用的指针的名称。我想我会 [像往常一样] 挖掘那个,但我很高兴有人知道如何给我答案(这会很好;))
Enjoy ? ;p
享受 ?;p
回答by rmarscher
There is a -no-cursor
option for Xorg 1.7 and later. https://www.x.org/wiki/AdvancedTopicsFAQ/
-no-cursor
Xorg 1.7 及更高版本有一个选项。https://www.x.org/wiki/AdvancedTopicsFAQ/
xinit -- -nocursor
or startx -- -nocursor
could work.
xinit -- -nocursor
或者startx -- -nocursor
可以工作。
回答by ILUXA
Use xbanish! It "banish the mouse cursor when typing"! Start it with
使用 xbanish!它“在打字时消除鼠标光标”!开始吧
xbanish &
xbanish &
and enjoy!
享受!