AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 4507
Accepted
levesque
levesque
Asked: 2010-09-22 07:26:23 +0800 CST2010-09-22 07:26:23 +0800 CST 2010-09-22 07:26:23 +0800 CST

如何禁用鼠标中键单击粘贴?

  • 772

有没有办法禁用 gnome 上默认的鼠标中键粘贴行为?

我有一个敏感的鼠标滚轮,每当我滚动文本时,有时它会将内容随机粘贴到文本中。当我将一个文件发送给其他人并且粘贴了随机文本片段时,我会失去很多可信度。

我见过一个解决方案,将鼠标的中间按钮映射到一个不存在的鼠标按钮,但这意味着完全摆脱鼠标中间按钮(即不关闭标签,自动打开链接到新标签等) . 我想保持鼠标中键处于活动状态,只需禁用粘贴行为。

当我用触摸板滚动文本时也会发生这种情况(不小心碰到两个手指而没有移动,bam。)

因此,仅通过更换新鼠标并不能解决问题(事实上,我相信触摸板比鼠标更常发生这种情况)。

xorg clipboard
  • 29 29 个回答
  • 151305 Views

29 个回答

  • Voted
  1. Sunny127
    2017-01-16T04:03:48+08:002017-01-16T04:03:48+08:00

    我使用 gnome-tweak-tool 在 Ubuntu 16.04 中禁用中间按钮粘贴。

    1. 安装它

      sudo apt install gnome-tweak-tool
      
    2. 通过在已安装的应用程序中搜索“调整工具”来运行它,或者只需gnome-tweak-tool在终端中输入。

    3. 转到“键盘和鼠标”->“中键粘贴”
    4. 关掉。

      截屏

    而已。

    或者只使用 CLI

    gsettings set org.gnome.desktop.interface gtk-enable-primary-paste false
    

    在 16.04 测试。

    • 88
  2. Ben
    2012-01-22T01:11:16+08:002012-01-22T01:11:16+08:00

    我意识到这不是您想要的答案,但是您可以在 Firefox 中将其关闭(例如,如果您不介意其他地方的功能,但仍希望在 Firefox 中单击鼠标中键以在新选项卡中打开链接)

    在 about:config 中,设置

    middlemouse.contentLoadURL false
    middlemouse.paste false
    

    不是你问的,但由于这个问题与几个地方有关,我希望有人觉得这个答案有用。

    • 65
  3. Jay Goldfarb
    2011-08-06T16:32:14+08:002011-08-06T16:32:14+08:00

    Jared Robinson 提供了一个适用于我的机器的简单解决方案:

    运行以下命令:

    xmodmap -e "pointer = 1 25 3 4 5 6 7 8 9"
    

    要保持此行为,请编辑 ~/.Xmodmap 并添加

    pointer = 1 25 3 4 5 6 7 8 9
    
    • 43
  4. Marco Ceppi
    2010-09-24T10:56:06+08:002010-09-24T10:56:06+08:00

    这目前是不可能的——不过,正如你所提到的,有一些方法可以禁用鼠标 3 按钮——或者重新映射它——这些方法都不是问题的根源。X11 主要选择。

    虽然这不是一个解决方案,但希望这个解释能说明为什么。在 Ubuntu 中,有两个剪贴板在工作。一个是大家都熟悉的 freedesktop.org 剪贴板(捕获Ctrl+C命令) 第二个是一个剪贴板管理器,它在 Ubuntu 存在之前就一直在发挥作用 - X11。X 服务器 (X11) 管理其他三个剪贴板:Primary Selection、Secondary Selection和Clipboard. 当您使用指针选择文本时,它会被复制到 XServer 中的缓冲区,即主选择,并等待通过鼠标 3 按钮进行粘贴。另外两个旨在供其他应用程序使用,以在应用程序之间共享公共剪贴板。在这种情况下,Ubuntu 中的 freedesktop.org 剪贴板管理器已经为我们完成了这项工作。

    根据我的研究范围,我找不到禁用 X11 选择管理器的方法。没有可以禁用此功能的编译标志、应用程序或配置值。在每个应用程序的基础上,有多种解决方法(这些应用程序中的大多数是命令行应用程序) - 但在全球范围内没有任何方法。

    我意识到这不是一个理想的解决方案 - 但似乎是问题的真相。我可以召集的唯一相关解决方案实际上是一个 hack,创建一个执行无限 while 循环的脚本,该循环只是将 Primary Selection 替换为 null 值。

    首次安装xsel(用于操作 X 选择的工具)sudo apt-get install xsel

    代码如下:

    while(true)
    do
        echo -n | xsel -n -i
        sleep 0.5
    done
    

    如果您将其放在脚本中并将其添加到您的启动脚本中,这应该不是问题。

    • 25
  5. Wes Miller
    2012-01-13T04:58:07+08:002012-01-13T04:58:07+08:00

    不知何故,我最终在我的 Ubuntu 安装上没有任何 xmodmap 文件,所以我不得不找到一种不同的方法来解决这个问题。

    看一下xinput命令。

    xinput list | grep -i mouse
    

    其中列出了有关您的鼠标的信息。它显示我的鼠标是“Dell Premium USB Optical Mouse”,而且我有“Macintosh mouse button emulation”。有了这些信息,我可以

    xinput get-button-map "Dell Premium USB Optical Mouse"
    

    这给了我一个看起来像的列表

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
    

    这是有用的,必需的知识。我的鼠标理论上有 18 个按钮。每个按钮的默认操作与其按钮编号具有相同的名称。换句话说,按钮 1 执行操作 1,按钮 4 执行操作 4,等等。操作 0 表示“关闭”。

    列表中的位置显示分配给该按钮的功能。所以如果我的按钮图读到

    1 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    

    这意味着按钮 1(位置 1)执行操作 1(普通左按钮),按钮 2(位置 2)执行操作 3(中间按钮),按钮 3(位置 3)执行操作 2(右键)。

    要制作左手鼠标,您只需要一个按钮映射即可

    3 2 1 4 5 .....
    

    或者,在您的情况下,您似乎希望中间按钮与按钮 1(左按钮)执行相同的操作,因此您的地图需要启动

    1 1 3 ....
    

    我会因此重置我的鼠标按钮映射:

    xinput set-button-map "Dell Premium USB Optical Mouse" 1 1 3 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18
    

    在您的情况下,您可能有不同数量的映射按钮,并且已经定义了一些特殊的按钮映射。Likwely,您的鼠标也有不同的名称。首先,获取鼠标的“名称”。然后,使用get-button-map操作找到您的基本按钮映射。最后,使用set-button-map选项,修改按钮 2 以执行操作 1。

    这不是永久性的变化。我将必要的代码添加到我的.bashrc中,这样每次我登录或打开终端时它都会执行。

    希望这可以帮助。

    • 21
  6. Best Answer
    Jelle Geerts
    2011-09-18T13:27:07+08:002011-09-18T13:27:07+08:00

    有关问题的解决方案,请查看我写的本指南:

    不久前,我写了一个补丁来禁用 GTK 中的“鼠标中键粘贴”功能。我认为会有其他人也想禁用它,因此我认为我应该写一个小指南来解释如何完成这个壮举。

    现在,有些人可能会问,为什么有人要禁用它?有几个原因:

    • 鼠标中键实际上并不是粘贴所谓的 XA_CLIPBOARD 剪贴板,而是粘贴 XA_PRIMARY 剪贴板。这对许多人来说可能是违反直觉的(也许是来自 Windows 的用户),因此有些人可能认为禁用“不一致”或意外行为对用户更友好。
    • 有些人可能会不小心使用鼠标中键粘贴文本,并希望避免这种情况。

    关于 XA_CLIPBOARD 和 XA_PRIMARY 剪贴板的一些背景知识: XA_PRIMARY 剪贴板主要用于存储选择。每当您在例如 GNOME 文本编辑器 (gedit) 中选择一些文本时,该文本都会被复制到 XA_PRIMARY 剪贴板。当您使用“编辑 -> 粘贴”菜单项时,不会粘贴此文本,仅当您单击鼠标中键时才会粘贴此文本。XA_CLIPBOARD 剪贴板主要用于使用一般的“复制/粘贴”功能(通过键盘快捷键,例如 CTRL+C 和 CTRL+V,或通过菜单项“编辑 -> 复制”和“编辑 -> 粘贴” )。

    也许这种补丁(或对 X 剪贴板和/或库/应用程序如何使用它的更剧烈的更改)有一天会成为 Linux 的标准。我意识到对此的看法有很大不同。但是,对于那些希望尽量减少意外粘贴一些随机文本的机会的人来说,该补丁可能非常有用。例如,使用补丁,您不会意外地将文本粘贴(至少使用鼠标中键)到您正在编辑的文档、网页或即时消息等中。

    该指南适用于 Debian 或基于 Debian 的 Linux 发行版,例如 Ubuntu 和 Mint。

    以下是终端命令及其作用的简要说明(以# 开头的行是注释,其中包含这些说明)。仔细阅读说明后,您应该启动一个终端并一一输入命令。

    # This is a small guide that explains how to patch GTK so that the middle mouse
    # button doesn't paste text anymore.
    
    # NOTE:
    # The below instructions are for GTK2. However, they should be easy to adapt
    # for GTK3 (at the time of writing, the patch works fine for GTK3 too).
    
    # First, update the system by first synchronizing the package index files, and
    # then upgrading the packages.
    sudo apt-get update
    sudo apt-get upgrade
    
    # Get the build dependencies and essential packages needed in order to compile
    # code and create packages.
    sudo apt-get build-dep libgtk2.0-0
    sudo apt-get install build-essential
    
    # Create a temporary directory, in which we will store the GTK sources and
    # later on the packages.
    mkdir /tmp/gtk
    cd /tmp/gtk
    
    # Download the actual patch that will disable the 'middle mouse button paste'
    # functionality (it should be stored in the directory '/tmp/gtk', and will be,
    # if you indeed executed the command 'cd /tmp/gtk').
    wget http://subversion.assembla.com/svn/slipstream/patches/gtk_disable_middle_mouse_button_paste.patch
    
    # Retrieve the GTK sources.
    apt-get source libgtk2.0-0
    
    # You should adapt this line so that it changes to the correct directory (the
    # name of the directory that I used here will probably not match the name of
    # the directory that was created during 'apt-get source libgtk2.0-0', as it
    # contains a version number that often changes). You can find out what the
    # correct directory is by entering 'ls -d */' (without the quotes) and looking
    # at the names of the directories that it shows.
    cd gtk+2.0-2.20.1
    
    # Apply the patch that we downloaded earlier.
    patch -p1 < /tmp/gtk/gtk_disable_middle_mouse_button_paste.patch
    
    # The output of the previous command should be:
    #     patching file gtk/gtkselection.c
    # If it wasn't, then something went wrong. Maybe you mistyped something, maybe
    # the current directory isn't the correct directory, maybe the GTK sources
    # were changed and the patch doesn't work anymore, etc.
    
    # Build the package (you may have to be patient, this may or may not take a
    # while).
    dpkg-buildpackage -uc -us --source-option=--auto-commit
    
    # You should adapt this line so that it installs the correct package. The
    # package that we want to install is the package containing the GTK library,
    # thus _not_ the 'bin', 'udeb', 'common', 'dev', or 'doc' package. To find out
    # what the exact package is that you should install, try to find the package (a
    # file with a name ending in '.deb') which is closest to the example filename I
    # used here (the packages are stored in '/tmp/gtk', and you can list the
    # packages using the command 'ls /tmp/gtk/*.deb' (without the quotes)).
    sudo dpkg -i ../libgtk2.0-0_2.20.1-2_i386.deb
    
    # And lastly, to make sure that only the patched library is in use, you should
    # either log out and back in, or restart your computer.
    # And then, the 'middle mouse button paste' functionality should be disabled.
    # To test whether it is, try selecting some text in the GNOME Text Editor, or
    # in a GNOME Terminal, and then press the middle mouse button while the cursor
    # hovers over some place where you can normally type text. If indeed no text
    # appears, then it appears that the patch worked.
    # If however, the patch did not work, try to re-read this document, to see if
    # you made any mistake. And if you did, you may want to either start all over
    # again (should be fail-safe), or continue with the guide from the point where
    # you made a mistake.
    

    或者,更直接地说,这是在 GTK 中禁用“鼠标中键粘贴”功能的补丁:

    diff -ur gtk+2.0-2.20.1/gtk/gtkselection.c gtk+2.0-2.20.1-patched/gtk/gtkselection.c
    --- gtk+2.0-2.20.1/gtk/gtkselection.c   2010-05-01 22:14:29.000000000 -0500
    +++ gtk+2.0-2.20.1-patched/gtk/gtkselection.c   2011-09-17 10:45:37.000000000 -0500
    @@ -1065,6 +1065,24 @@
       display = gtk_widget_get_display (widget);
       owner_window = gdk_selection_owner_get_for_display (display, selection);
    
    +  if (selection == gdk_atom_intern("PRIMARY", TRUE)) {
    +      GtkSelectionData selection_data;
    +
    +      selection_data.selection = selection;
    +      selection_data.target = target;
    +      selection_data.type = gdk_atom_intern("STRING", TRUE);
    +      selection_data.format = 8;
    +      selection_data.data = (unsigned char *)"";
    +      selection_data.length = 0;
    +      selection_data.display = display;
    +
    +      gtk_selection_retrieval_report(info, selection_data.type,
    +              selection_data.format, selection_data.data,
    +              selection_data.length, time_);
    +
    +      return TRUE;
    +  }
    + 
       if (owner_window != NULL)
         {
           GtkWidget *owner_widget;
    
    • 18
  7. Kvothe
    2019-05-18T05:09:44+08:002019-05-18T05:09:44+08:00

    对我有用的唯一答案是在https://unix.stackexchange.com/a/277488/288916 Radivarig 上给出的(所有学分归他所有)。请注意,我必须进行一项更改才能使其正常工作:

    安装 xbindkeys:

    sudo apt-get install xbindkeys xsel xdotool
    

    使用以下文本为 xbindkeys 创建一个配置文件 ~/.xbindkeysrc:

    "echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"
    b:2
    

    使用加载配置文件

    xbindkeys -p
    

    添加此行,~./bashrc以便xbindkeys始终在启动时加载。

    这对我有用,但 Radivarig 建议改用这条线

    "echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"
        b:2 + Release
    

    最后一个对我不起作用,但如果其中一个版本不起作用,我建议尝试另一个。

    经过长时间的寻找,这是我发现对我有用的唯一解决方案,它可以普遍关闭鼠标中键粘贴按钮,而无需完全禁用鼠标中键。


    请注意,对于现有的投票答案,要么他们不回答问题,而是告诉您如何完全禁用鼠标中键,或者他们提供仅适用于少数程序的解决方案(仅在 gedit 和gnome 终端和其他少数人)或者他们只是说这是不可能的。


    spawn 的回答具有类似的精神,也可能有效,在我找到这个解决方案之前我没有看到它。

    • 13
  8. Cestarian
    2018-10-01T14:02:21+08:002018-10-01T14:02:21+08:00

    编辑:在我看来,以下答案是比我更好的解决方案:https ://unix.stackexchange.com/a/501445/72554

    这是我发现的最简单的解决方案,可以在不粘贴内容的情况下保持中间点击功能。

    首先安装sxhkd和xsel。

    然后像这样配置~/.config/.sxhkd/sxhkdrc

    ~button2
        echo -n | xsel -n -i
    ~control + c
        echo -n | xsel -n -i
    ~control + x
        echo -n | xsel -n -i
    

    基本上就是这样。

    现在启动 sxhkd(或将其配置为在启动时启动)。现在,鼠标中键将不再粘贴任何内容,但会正常工作。(只要你只使用 ctrl+c 和 ctrl+x 来复制东西,如果你用鼠标复制东西,它可以在恢复正常之前粘贴一次。)

    • 6
  9. Ilario Gelmetti
    2019-02-02T02:31:01+08:002019-02-02T02:31:01+08:00

    对于 Gnome 应用程序,您可以在“键盘和鼠标”选项卡下使用gnome-tweaks(gnome-tweak-tool包的新名称)有“中键单击粘贴”选项或直接编辑 org.gnome.desktop.interface/gtk-enable-primary-paste Gnome 选项。

    对于 KDE 应用程序,似乎有一个等效的解决方案。

    对于整个 X(包括非 Gnome 应用程序),您可以安装XMousePasteBlock,然后它必须运行(用户就足够了,不需要 root)才能工作。这将完全禁用中间点击粘贴,而不会禁用其他中间点击功能。

    • 5
  10. spawn
    2016-02-23T03:10:30+08:002016-02-23T03:10:30+08:00

    我建议使用其中一种,这对我来说最有效:

    使用 xbindkeys:每当按下中间按钮时,清除主剪贴板。至少在我的系统上,在粘贴之前它被清除了。详细信息:创建 xbindkeys-config:

    xbindkeys --defaults > $HOME/.xbindkeysrc
    

    粘贴以下新热键:

    "xclip -i /dev/null"    
        b:2``
    

    重新加载 xbindkeys(例如 killall xbindkeys;xbindkeys)。完毕。

    使用 xdotool:在窗口焦点更改时清除剪贴板(应该适用于大多数窗口管理器)。详细信息:执行以下命令:

    xdotool search --onlyvisible . behave %@ focus exec xclip -i /dev/null
    

    请注意,使用此命令,您仍然可以在同一窗口中使用主剪贴板,或者在聚焦之前将鼠标中键按到另一个窗口上(如果您没有激活“焦点跟随鼠标”或其他功能)。

    • 5

相关问题

  • 如何从命令行重新启动 X Window Server?

  • xorg 中是否有用于更改视频驱动程序的 GUI?

  • 类似于 Eyefinity 的东西?

  • 如何重置我的键盘布局?

  • 帮助让 Flash 播放器在第二个屏幕上工作?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve