如何更改每个应用程序的鼠标滚轮滚动速度

437

是否有可能基于在顶部(集中)运行的应用程序而具有不同的鼠标滚轮滚动速度。

例如,较慢的guake滚动速度(易于阅读)和较高的Web浏览器滚动速度(例如)。

塞尔吉(Sergiy Kolodyazhnyy)

介绍

以下脚本dynamic_mouse_speed.py允许指定当用户定义的窗口具有焦点时应该是什么鼠标指针和/或滚动速度。

重要提示:该脚本需要imwheel程序才能提高滚动速度。请通过安装sudo apt-get install imwheel

用法

-h标志所示

usage: dynamic_mouse_speed.py [-h] [-q] [-p POINTER] [-s SCROLL] [-v]

Sets mouse pointer and scroll speed per window

optional arguments:
  -h, --help            show this help message and exit
  -q, --quiet           Blocks GUI dialogs.
  -p POINTER, --pointer POINTER
                        mouse pointer speed,floating point number from -1 to 1
  -s SCROLL, --scroll SCROLL
                        mouse scroll speed,integer value , -10 to 10
                        recommended
  -v, --verbose         prints debugging information on command line

该脚本允许用户通过单击鼠标来选择要跟踪的窗口。鼠标指针将变成十字形,用户可以选择所需的窗口。

python3 dynamic_mouse_speed.py单独运行仅显示弹出对话框,并且本身不执行任何操作。

运行可python3 dynamic_mouse_speed.py -s 5提高滚动速度,而python3 dynamic_mouse_speed.py -s -5降低滚动速度。python3 dynamic_mouse_speed.py -p -0.9降低指针速度,同时python3 dynamic_mouse_speed.py -p 0.9提高指针速度。-s-p选项可以混合使用。-v在命令行上生成调试信息。

来源

也可以作为GitHub gist获得

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author: Sergiy Kolodyazhnyy
Date:  August 2nd, 2016
Written for: https://askubuntu.com/q/806212/295286
Tested on Ubuntu 16.04 LTS

usage: dynamic_mouse_speed.py [-h] [-q] [-p POINTER] [-s SCROLL] [-v]

Sets mouse pointer and scroll speed per window

optional arguments:
  -h, --help            show this help message and exit
  -q, --quiet           Blocks GUI dialogs.
  -p POINTER, --pointer POINTER
                        mouse pointer speed,floating point number from -1 to 1
  -s SCROLL, --scroll SCROLL
                        mouse scroll speed,integer value , -10 to 10
                        recommended
  -v, --verbose         prints debugging information on command line


"""
from __future__ import print_function
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk, Gtk,Gio
import time
import subprocess
import sys
import os
import argparse


def run_cmd(cmdlist):
    """ Reusable function for running shell commands"""
    try:
        stdout = subprocess.check_output(cmdlist)
    except subprocess.CalledProcessError:
        print(">>> subprocess:",cmdlist)
        sys.exit(1)
    else:
        if stdout:
            return stdout



def get_user_window():
    """Select two windows via mouse. 
       Returns integer value of window's id"""
    window_id = None
    while not window_id:
        for line in run_cmd(['xwininfo', '-int']).decode().split('\n'):
            if 'Window id:' in line:
                window_id = line.split()[3]
    return int(window_id)

def gsettings_get(schema,path,key):
    """Get value of gsettings schema"""
    if path is None:
        gsettings = Gio.Settings.new(schema)
    else:
        gsettings = Gio.Settings.new_with_path(schema,path)
    return gsettings.get_value(key)

def gsettings_set(schema,path,key,value):
    """Set value of gsettings schema"""
    if path is None:
        gsettings = Gio.Settings.new(schema)
    else:
        gsettings = Gio.Settings.new_with_path(schema,path)
    return gsettings.set_double(key,value)

def parse_args():
    """ Parse command line arguments"""
    arg_parser = argparse.ArgumentParser(
                 description="""Sets mouse pointer and scroll """ + 
                             """speed per window """)
    arg_parser.add_argument(
                '-q','--quiet', action='store_true',
                help='Blocks GUI dialogs.',
                required=False)

    arg_parser.add_argument(
                '-p','--pointer',action='store',
                type=float, help=' mouse pointer speed,' + 
                'floating point number from -1 to 1', required=False)

    arg_parser.add_argument(
                '-s','--scroll',action='store',
                type=int, help=' mouse scroll speed,' + 
                'integer value , -10 to 10 recommended', required=False)

    arg_parser.add_argument(
                '-v','--verbose', action='store_true',
                help=' prints debugging information on command line',
                required=False)
    return arg_parser.parse_args()

def get_mouse_id():
    """ returns id of the mouse as understood by
        xinput command. This works only with one
        mouse attatched to the system"""
    devs = run_cmd( ['xinput','list','--id-only']   ).decode().strip()
    for dev_id in devs.split('\n'):
        props = run_cmd( [ 'xinput','list-props', dev_id  ]   ).decode()
        if "Evdev Scrolling Distance" in props:
            return dev_id


def write_rcfile(scroll_speed):
    """ Writes out user-defined scroll speed
        to ~/.imwheelrc file. Necessary for
        speed increase"""

    number = str(scroll_speed)
    user_home = os.path.expanduser('~')
    with open( os.path.join(user_home,".imwheelrc") ,'w'  ) as rcfile:
        rcfile.write( '".*"\n' )
        rcfile.write("None, Up, Button4, " + number + "\n"   )   
        rcfile.write("None, Down, Button5, " + number + "\n")
        rcfile.write("Control_L, Up,   Control_L|Button4 \n" +
                     "Control_L, Down, Control_L|Button5 \n" +
                     "Shift_L,   Up,   Shift_L|Button4 \n" +
                     "Shift_L,   Down, Shift_L|Button5 \n" )



def set_configs(mouse_speed,scroll_speed,mouse_id):
    """ sets user-defined values
        when the desired window is in focus"""
    if mouse_speed:
        gsettings_set('org.gnome.desktop.peripherals.mouse',None, 'speed', mouse_speed)

    if scroll_speed:
       if scroll_speed > 0:
           subprocess.call(['killall','imwheel'])
           # Is it better to write config here
           # or in main ?
           write_rcfile(scroll_speed)
           subprocess.call(['imwheel'])
       else:
           prop="Evdev Scrolling Distance"
           scroll_speed = str(abs(scroll_speed))
           run_cmd(['xinput','set-prop',mouse_id,prop,scroll_speed,'1','1']) 



def set_defaults(mouse_speed,scroll_speed,mouse_id):
    """ restore values , when user-defined window
        looses focus"""
    if mouse_speed:
        gsettings_set('org.gnome.desktop.peripherals.mouse', None, 
                      'speed', mouse_speed)

    if scroll_speed:
        if scroll_speed > 0:
           subprocess.call(['killall','imwheel'])
        if scroll_speed < 0:
           prop="Evdev Scrolling Distance"
           run_cmd(['xinput','set-prop',mouse_id,prop,'1','1','1'])


def main():
    """Entry point for when program is executed directly"""
    args = parse_args()

    # Get a default configs
    # gsettings returns GVariant, but
    # setting same schema and key requires 
    # floating point number
    screen = Gdk.Screen.get_default()
    default_pointer_speed = gsettings_get('org.gnome.desktop.peripherals.mouse', 
                                          None, 
                                          'speed')
    default_pointer_speed = float(str(default_pointer_speed))


    # Ask user for values , or check if those are provided via command line
    if not args.quiet:
       text='--text="Select window to track"'
       mouse_speed = run_cmd(['zenity','--info',text])

    user_window = get_user_window() 

    scroll_speed = args.scroll    
    pointer_speed = args.pointer
    mouse_id = get_mouse_id()

    if pointer_speed: 
        if pointer_speed > 1 or pointer_speed < -1:

           run_cmd(['zenity','--error',
                    '--text="Value out of range:' + 
                    str(pointer_speed) + '"'])
           sys.exit(1)

    # ensure that we will raise the user selected window
    # and activate all the preferences 
    flag = True
    for window in screen.get_window_stack():
        if user_window == window.get_xid():
            window.focus(time.time())
            window.get_update_area()
    try:
        while True:
            time.sleep(0.25) # Necessary for script to catch active window
            if  screen.get_active_window().get_xid() == user_window:
                if flag:
                    set_configs(pointer_speed,scroll_speed,mouse_id) 
                    flag=False

            else:
               if not flag:
                  set_defaults(default_pointer_speed, scroll_speed,mouse_id)
                  flag = True

            if args.verbose: 
                print('ACTIVE WINDOW:',str(screen.get_active_window().get_xid()))
                print('MOUSE_SPEED:', str(gsettings_get(
                                          'org.gnome.desktop.peripherals.mouse',
                                           None, 'speed')))
                print('Mouse ID:',str(mouse_id))
                print("----------------------")
    except:
        print(">>> Exiting main, resetting values")
        set_defaults(default_pointer_speed,scroll_speed,mouse_id)

if __name__ == "__main__":
    main()

笔记

  • 脚本的多个实例允许在每个单独的窗口中设置速度。
  • 从命令行运行时,弹出对话框会产生以下消息:Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.这些可以忽略。
  • 咨询如何在Unity中手动编辑/创建新的启动器项目?用于为此脚本创建启动器或桌面快捷方式,如果您需要双击启动它
  • 为了将此脚本链接到键盘快捷键以便于访问,请参阅如何添加键盘快捷键?
  • 建议您在运行脚本时仅使用一个鼠标,因为它在发现具有Evdev Scrolling Distance属性的第一个设备上运行
  • 可以启动多个实例来控制多个窗口,但是出于性能考虑,不建议这样做

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Sublime Text 3中更改鼠标滚轮滚动速度?

来自分类Dev

增加鼠标滚轮滚动速度

来自分类Dev

增加鼠标滚轮滚动速度

来自分类Dev

使用鼠标滚轮在桌面上的Sencha Touch应用程序中滚动

来自分类Dev

Mac OS X 使用鼠标滚轮放大应用程序

来自分类Dev

在Chrome 12.04上更改鼠标滚轮滚动速度(编辑启动栏命令行)

来自分类Dev

如何更改应用程序抽屉出现的速度?

来自分类Dev

路径应用程序更改时如何停止滚动事件?

来自分类Dev

如何获得鼠标滚轮的垂直滚动速率

来自分类Dev

如何让鼠标滚轮在zsh上滚动屏幕?

来自分类Dev

在FireFox中,Iscroll鼠标滚轮滚动的速度比Google Chrome慢

来自分类Dev

鼠标滚轮速度

来自分类Dev

如何在控制台应用程序中启用鼠标滚动

来自分类Dev

如何在控制台应用程序中启用鼠标滚动

来自分类Dev

鼠标滚轮水平滚动

来自分类Dev

如何使鼠标滚轮在鼠标光标下滚动控制?

来自分类Dev

在运行应用程序时启用滚轮滚动命令提示符

来自分类Dev

如何在SWT中使用鼠标滚轮滚动滚动的合成

来自分类Dev

如何通过鼠标滚轮水平滚动div,锁定身体滚动?

来自分类Dev

如何使用adb滚动应用程序?

来自分类Dev

如何使鼠标滚轮滚动到mediafire.com中的部分

来自分类Dev

如何在TDBCtrlGrid上启用鼠标滚轮滚动?

来自分类Dev

如何使用鼠标滚轮在scrollviewer中滚动列表框

来自分类Dev

如何使子TButton散焦以使TScrollbox用鼠标滚轮滚动

来自分类Dev

如何在jQuery中计算鼠标滚轮滚动

来自分类Dev

如何在Ubuntu 11.04或10.10中禁用鼠标滚轮滚动?

来自分类Dev

如何用鼠标滚轮水平滚动表格反应?

来自分类Dev

如何在Ubuntu 11.04或10.10中禁用鼠标滚轮滚动?

来自分类Dev

如何使用键盘通过AutoHotkey模拟鼠标滚轮滚动?

Related 相关文章

  1. 1

    如何在Sublime Text 3中更改鼠标滚轮滚动速度?

  2. 2

    增加鼠标滚轮滚动速度

  3. 3

    增加鼠标滚轮滚动速度

  4. 4

    使用鼠标滚轮在桌面上的Sencha Touch应用程序中滚动

  5. 5

    Mac OS X 使用鼠标滚轮放大应用程序

  6. 6

    在Chrome 12.04上更改鼠标滚轮滚动速度(编辑启动栏命令行)

  7. 7

    如何更改应用程序抽屉出现的速度?

  8. 8

    路径应用程序更改时如何停止滚动事件?

  9. 9

    如何获得鼠标滚轮的垂直滚动速率

  10. 10

    如何让鼠标滚轮在zsh上滚动屏幕?

  11. 11

    在FireFox中,Iscroll鼠标滚轮滚动的速度比Google Chrome慢

  12. 12

    鼠标滚轮速度

  13. 13

    如何在控制台应用程序中启用鼠标滚动

  14. 14

    如何在控制台应用程序中启用鼠标滚动

  15. 15

    鼠标滚轮水平滚动

  16. 16

    如何使鼠标滚轮在鼠标光标下滚动控制?

  17. 17

    在运行应用程序时启用滚轮滚动命令提示符

  18. 18

    如何在SWT中使用鼠标滚轮滚动滚动的合成

  19. 19

    如何通过鼠标滚轮水平滚动div,锁定身体滚动?

  20. 20

    如何使用adb滚动应用程序?

  21. 21

    如何使鼠标滚轮滚动到mediafire.com中的部分

  22. 22

    如何在TDBCtrlGrid上启用鼠标滚轮滚动?

  23. 23

    如何使用鼠标滚轮在scrollviewer中滚动列表框

  24. 24

    如何使子TButton散焦以使TScrollbox用鼠标滚轮滚动

  25. 25

    如何在jQuery中计算鼠标滚轮滚动

  26. 26

    如何在Ubuntu 11.04或10.10中禁用鼠标滚轮滚动?

  27. 27

    如何用鼠标滚轮水平滚动表格反应?

  28. 28

    如何在Ubuntu 11.04或10.10中禁用鼠标滚轮滚动?

  29. 29

    如何使用键盘通过AutoHotkey模拟鼠标滚轮滚动?

热门标签

归档