0

I'm trying to add a keybinding to my rc.lua for shutting down my computer, which will display a prompt like "Shutdown (y/n)? ", and will call a shutdown if "y" is pressed, but will close if anything else is pressed. Here's my attempt so far:

...
awful.key({ modkey, "Control", "Shift" }, "q",
      function ()
          awful.prompt.run {
            prompt       = "Shutdown? (y/n) ",
            textbox      = awful.screen.focused().mypromptbox.widget,
            keypressed_callback = function (_, key, _)
                if key == "y" then
                    naughty.notify {
                        text = "Shutting down!"
                    }
                else
                    naughty.notify {
                        text = "Not shutting down!"
                    }
                end
                return
            end,
          }
      end,
  {description = "shutdown", group = "awesome"}),
...

However, the prompt remains active after keys are pressed - the keypressed_callback will continue to trigger every another key is pressed, until I press either Return or Escape.

This is sensible default behaviour, but in my case I want the prompt to close after the first keypressed_callback event. My first thought was to use a return within the keypressed_callback to try to escape/cancel/destroy the prompt, but that isn't doing anything.

Is there anyway to achieve this?

murchu27
  • 527
  • 2
  • 6
  • 20

1 Answers1

0

You can call awful.keygrabber.stop().

I must admit this isn't ideal and that function is actually deprecated. I think this is indeed a feature gap in the prompt module itself.

What I would propose for single character prompts is to use https://awesomewm.org/apidoc/core_components/awful.keygrabber.html directly and implement a non-interactive widget using the textbox.

(another alternative is to use root.fake_input or awful.keyboard to emulate Escape/Enter, but that's a very bad hack)

  • When you say a "non-interactive widget", do you mean without using `awful.prompt.run()`? Can you suggest how I could do that? I did make an attempt with using `awful.prompt.run()`, but it has two problems. Firstly, the keygrabber doesn't always stop after the first key is pressed, and sometimes I can press 2 or 3 keys before it stops. Can't figure out why. Also, the prompt doesn't disappear after calling `awful.keygrabber.stop()`, and I can't figure out how to clear it. – murchu27 May 15 '21 at 09:37
  • What I mean is using `awful.keygrabber` instead of the prompt widget for this use case. It isn't perfect, but makes "new use cases" (such as single character prompt) easier to implement. The `awful.prompt.run` API had some paint coats over the years, but under the hood, much of it is like 13 years old and pretty much restricted to what is does today. – Emmanuel Lepage Vallee May 17 '21 at 05:10