Sublime Forum

Get single character input

#1

Is there a way for a plugin to get single character input?

0 Likes

#2

Can you expand a bit on what you’re trying to accomplish?

In general anything that allows you to prompt the user for input interactively requires that they press a minimum of two keys (the single character of input, and Enter to submit it). So in that regard, I don’t think there’s a way to do something like this.

That said, depending on your use case you may be able to make use of key bindings and a custom context to provide single character input.

0 Likes

#3

I’m trying to emulate a behavior of an adaptive editor for handicapped people the provide typing short cuts be it requires me to preprocess keystrokes

trying to setup a programming tutorial

0 Likes

#4

I think the closest you could get to something like that would be a series of key bindings that cover all of the keys that you want to handle and a custom context that makes them only apply when you want them to. Then your code would be informed of every key press and you could handle them as you like.

However depending on the scope of this, that would be a lot of key bindings so that may be a little unweildy. You also can’t currently create a key binding on just a modifier (say to know when someone pressed just Ctrl). It’s been mentioned by Jon on Discord that this capability is coming to a future version, though.

0 Likes

#5

There is package for a Sublime Text “Tutorial”: https://packagecontrol.io/packages/Sublime%20Tutor

0 Likes

#6

You may add a key binding to "keys": ["<character>"], which will call the specified command with an additional character argument. This binding is active for every produced glyph and it cannot be combined with modifiers.

Make sure to add a proper context as otherwise you won’t be able to type normally anymore.

1 Like

#7

This is a rather old topic, but google lead me to it as I was asking myself the same question. Maybe I am missing the point, but I do not understand why one cannot get rid of the mandatory Enter key in the input panel as in the following simple example:

class InsertACharacterFiveTimesCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.view = self.window.active_view()
        self.window.show_input_panel(
            "Enter a letter: ", "",
            None, self.on_change, None
        )

    def on_change(self, char):
        self.window.run_command("hide_panel", {"cancel": True})
        view = self.window.active_view()
        view.run_command("insert", {"characters": char*5})

Since people in this forum are way more competent than I am, I suspect that it is not advisable to write such a code. Is it the case ?

0 Likes

#8

Not sure what problem you’re seeing, but that code opens the panel and then when you press a single character closes the panel and inserts that character 5 times; is that not what you want to happen?

0 Likes

#9

Thank you very much for your answer. I just wanted to check that this is a reliable method to get single-character input, before using it for something else (the 5-characters insertion is just a example for testing). From your answer I gather that there is no major issue in doing so. Thanks again.

0 Likes