Home Download Buy Blog Forum Support

How to get typed key name?

How to get typed key name?

Postby tatatamk on Mon Jul 14, 2014 3:54 pm

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?
tatatamk
 
Posts: 3
Joined: Mon Jul 14, 2014 3:31 pm

Re: How to get typed key name?

Postby Jim on Mon Jul 14, 2014 6:47 pm

tatatamk wrote:I want to get key name which user types, "a", "s", "d", "enter", "space" and others...


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:
asd<enter>space

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:
https://www.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.
Jim (on Windows Win7, now finally with ST3)
Jim
 
Posts: 40
Joined: Sun Feb 26, 2012 5:21 pm

Re: How to get typed key name?

Postby tatatamk on Tue Jul 15, 2014 2:52 pm

Thank you for your response, Jim.

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


I want to treat key-name as a variable in the program.
tatatamk
 
Posts: 3
Joined: Mon Jul 14, 2014 3:31 pm

Re: How to get typed key name?

Postby Jim on Tue Jul 15, 2014 3:20 pm

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: Select all
myKey = sublime.getkeypress()
if myKey == "enter":
    print("Enter key pressed.")
    do action on_enter_function
elif myKey == "a":
   do action on_a_function



:roll: :?:
Jim (on Windows Win7, now finally with ST3)
Jim
 
Posts: 40
Joined: Sun Feb 26, 2012 5:21 pm

Re: How to get typed key name?

Postby tatatamk on Wed Jul 16, 2014 4:41 pm

@Jim

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

Code: Select all
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])
...
tatatamk
 
Posts: 3
Joined: Mon Jul 14, 2014 3:31 pm


Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests