简体   繁体   中英

How to configure 5 buttons mouse in Sublime Text 3

I have a mouse with 5 buttons, how can I configure these buttons to do specific tasks in Sublime Text 3. Like when button4 does Build and button5 does Build With...

In case of Sublime Text, the mouse actions are configured by what are known as mousemap files (that have an extension .sublime-mousemap ). You can have generally 2 variants of these files:-

  • Default.sublime-mousemap : This will define mouse actions for any platform.
  • Default ($platform).sublime-mousemap : This will define mouse actions for a specific platform, where $platform is any one of Windows , Linux or OSX depending on your operating system.

You can view the default shipped mousemap files by using View Package File from the command palette and searching for mousemap .

In order to define your own mouse actions (or override any existing actions), you have to create a file by the name of Default.sublime-mousemap in the User directory (to get to this directory, select Preferences -> Browse Packages... from the main menu) for platform independent override (or Default ($platform).sublime-mousemap for platform dependent overrides depending on your OS).

Once that's done, here is some basic knowledge about mousemap files (Note that there is no official or community documentation about mousemap files so everything is based on experimentation and what the dev's have said about such files).

Here are the meaning of some keys in mousemap files:-

  • button : This defines the name of the button. For example, button1 refers to the left mouse button & button2 defines the right mouse button. Similarly, you can have button3 , button4 . button5 etc. I am not sure how many such button names actually exist. Also for the scroll wheel, you have scroll_up for upward scroll movement & scroll_down for the opposite behavior.

  • modifiers : This is a list of modifier keys like ctrl , alt etc. For example, ["alt"] , ["ctrl", "alt"] . When you define a modifier list, all the modifier keys listed should be pressed simultaneously and then pressing/releasing the corresponding button triggers some action.

  • command : This defines the command to be executed when the corresponding button is released after being pressed. If this command takes any arguments, you can have an args key for it.

  • press_command : This defines the command to be executed when the corresponding button is pressed . If this command takes any arguments, you can have a press_args key for it.

  • count : The number of times you have to press the corresponding button to trigger the action (by action, I mean execute the corresponding command / press_command )

NOTE: You can define both command and press_command if you wanted to.

Let's look at some examples:-

File name:- User/Default.sublime-mousemap

[
    {
        "button": "button2",
        "modifiers": [],
        "press_command": "echo",
        "press_args": {
            "message": "I am pressed"
        },
        "command": "echo",
        "args": {
            "message": "I am released"
        },
    }
]

Here, the right button ( button2 ) is bound to the built in echo command. If you now right click, the default behavior would have been actually the opening of the context menu, but now we have overriden that behavior and now you can see the corresponding messages {'message': 'I am pressed'} or {'message': 'I am released'} in the console based on whether you have pressed or released after pressing.

For your case, you can have something like the following:-

[
    {
        "button": "button1", // replace button1 with button4/5 because I don't have that many mouse buttons.
        "modifiers": ["alt", "ctrl", "shift"],
        "press_command": "undo",
    }   
]

Now, when you now press button1 (while holding down alt , ctrl , shift simultaneously), the undo command should be executed. You can set modifiers to an empty list if you don't want that.

As for build, if you mean executing the most recent build system, replace undo , with build .

As a parting bonus tip, if you want to disable any button actions, just use the command noop .

Example:-

[
    {
        "button": "button1",
        "modifiers": [],
        "press_command": "noop",
    }   
]

This will disable button1 and now you can't drag select anymore;-) So be careful.

Hopefully, this helps a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM