Sublime Forum

How to get typed key name?

#1

I want to get key name which user types, “a”, “s”, “d”, “enter”, “space” and others…
I think, on_modified event is suitable for this, but its event gets view object,
and view object hasn’t methods for getting key name pressed.

Then, I have tried another idea.
I bound all keys to commands which are defined newly.
However, after I have tried that, when I entered the key, the key was not inputted.

What would be the best thing to do?

0 Likes

How to autocomplete inside a word?
#2

Do you mean you want to see what ST gets if a user press a specific key on the keyboard?

Then open ST Console “View > Console”

And type in:
sublime.log_input(True)

Then type something into ST:
asdspace

and watch the console log:
chr evt: a (0x61)
chr evt: s (0x73)
chr evt: d (0x64)
key evt: enter
chr evt: (0x20)

To disable the logging, type into the console
sublime.log_input(False)

For more see:
sublimetext.com/docs/2/api_ … ntListener
Module sublime
log_commands(flag) None Controls command logging. If enabled, all commands run from key bindings and the menu will be logged to the console.
log_input(flag) None Controls input logging. If enabled, all key presses will be logged to the console.

0 Likes

#3

Thank you for your response, Jim.

I want to treat key-name as a variable in the program.

0 Likes

#4

Sorry, I still don’t get it.

“”“Take a key-name as a variable in the program”"" ???

Would you please provide an example what you are after?

Do you mean to write a script which interacts depending on the pressed keyboard key?

Something like this dummy code:[code]myKey = sublime.getkeypress()
if myKey == “enter”:
print(“Enter key pressed.”)
do action on_enter_function
elif myKey == “a”:
do action on_a_function

[/code]

:unamused: :question:

1 Like

#5

@Jim

I’m developing joke plugin which swaps 2 keys.
I pressed key “A” then, sublime inputs “L” on window

key_map = { "A": "L", "S": "K", "D": "J"... }

class Swap(sublime_plugin.EventListener):
    def on_modified(self, view):
        point = view.sel()[0].begin()
        my_key = sublime.get_keypress()
        view.insert(edit, point, key_map[my_key])
...
0 Likes

#6

You can do this:

import sublime
import sublime_plugin

class OpenAutoCompletionCommand(sublime_plugin.TextCommand):

    def run(self, edit, **kargs):
        # print( "kargs: ", str( kargs ) )

        view = self.view

        if kargs["keystroke"] == 'a':
            view.run_command("insert", {"characters": "l"})

        elif kargs["keystroke"] == 'l':
            view.run_command("insert", {"characters": "a"})

        else:
            view.run_command("insert", {"characters": kargs["keystroke"]})

Which needs this keymap file Default.sublime-keymap:

[
    { "keys": ["a"], "command": "open_auto_completion", "args": {"keystroke": "a" } },
    { "keys": ["b"], "command": "open_auto_completion", "args": {"keystroke": "b" } },
    { "keys": ["c"], "command": "open_auto_completion", "args": {"keystroke": "c" } },
    { "keys": ["d"], "command": "open_auto_completion", "args": {"keystroke": "d" } },
    { "keys": ["e"], "command": "open_auto_completion", "args": {"keystroke": "e" } },
    { "keys": ["f"], "command": "open_auto_completion", "args": {"keystroke": "f" } },
    { "keys": ["g"], "command": "open_auto_completion", "args": {"keystroke": "g" } },
    { "keys": ["h"], "command": "open_auto_completion", "args": {"keystroke": "h" } },
    { "keys": ["i"], "command": "open_auto_completion", "args": {"keystroke": "i" } },
    { "keys": ["j"], "command": "open_auto_completion", "args": {"keystroke": "j" } },
    { "keys": ["k"], "command": "open_auto_completion", "args": {"keystroke": "k" } },
    { "keys": ["l"], "command": "open_auto_completion", "args": {"keystroke": "l" } },
    { "keys": ["m"], "command": "open_auto_completion", "args": {"keystroke": "m" } },
    { "keys": ["n"], "command": "open_auto_completion", "args": {"keystroke": "n" } },
    { "keys": ["o"], "command": "open_auto_completion", "args": {"keystroke": "o" } },
    { "keys": ["p"], "command": "open_auto_completion", "args": {"keystroke": "p" } },
    { "keys": ["q"], "command": "open_auto_completion", "args": {"keystroke": "q" } },
    { "keys": ["r"], "command": "open_auto_completion", "args": {"keystroke": "r" } },
    { "keys": ["s"], "command": "open_auto_completion", "args": {"keystroke": "s" } },
    { "keys": ["t"], "command": "open_auto_completion", "args": {"keystroke": "t" } },
    { "keys": ["u"], "command": "open_auto_completion", "args": {"keystroke": "u" } },
    { "keys": ["v"], "command": "open_auto_completion", "args": {"keystroke": "v" } },
    { "keys": ["w"], "command": "open_auto_completion", "args": {"keystroke": "w" } },
    { "keys": ["x"], "command": "open_auto_completion", "args": {"keystroke": "x" } },
    { "keys": ["y"], "command": "open_auto_completion", "args": {"keystroke": "y" } },
    { "keys": ["z"], "command": "open_auto_completion", "args": {"keystroke": "z" } },
]
0 Likes

#7

or just bind to "keys": ["<character>"] and you’ll get an arg with the character automagically

2 Likes