I'd like to write a simple loop that catches and prints the incoming event while the script is running. I've tried this:

       (let ((key 0))
         (while (= key 0)
           (setq event (read-char nil nil 0))
           (if event (print event))))

The script runs but no event data is printed. After C-g all characters pressed were printed in the buffer. What's wrong?


1 Answer 1


Passing a value of 0 for the SECONDS arg of read-char means it will immediately return nil, always.

(And your code never changes the value of key, so it remains 0. So the iteration goes on forever, till you hit C-g. Maybe that's what you wanted, though.)

(defun foo ()
  (let ((key 0))
    (while (= key 0)
      (setq event (read-char "Char: " nil 5.0))
      (if event (print event)))))

Look in buffer *Messages* to see the chars read and printed.

C-h f read-char says, about SECONDS:

If the optional argument SECONDS is non-nil, it should be a number specifying the maximum number of seconds to wait for input. If no input arrives in that time, return nil. SECONDS may be a floating-point value.

  • Is there a way to check if there was an event not resolved without awaiting? I'd like to create a loop that just check if a key is pressed and if not the script do other stuff.
    – Quasar999
    Apr 1, 2022 at 18:36
  • Please don't ask new questions or evolve your question in comments. One question per post - please post another question if you have one. Thx.
    – Drew
    Apr 1, 2022 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.