2022 Developer Survey is open! Take survey.

Hot answers tagged

27 votes
Accepted

How can I simulate an arbitary key event from Elisp?

You can feed arbitrary events (keystrokes, mouse clicks, etc.) to the command loop by putting them onto unread-command-events. For example, the following will cause the command loop to execute a ...
user avatar
  • 5,480
14 votes

How can I simulate an arbitary key event from Elisp?

The simplest way I know of is just to use execute-kbd-macro: (defun foo () (interactive) (execute-kbd-macro (kbd "<escape>"))) (global-set-key (kbd "C-`") 'foo)
user avatar
  • 4,950
7 votes
Accepted

Looking for something like a `first-keypress-in-a-while-hook`

You could try: (defun my-run-fkpiawh () (remove-hook 'pre-command-hook #'my-run-fkpiawh) (run-hooks 'first-keypress-in-a-while-hook)) (run-with-idle-timer 1200 t (lambda () ...
user avatar
  • 25.2k
6 votes

Looking for something like a `first-keypress-in-a-while-hook`

I guess a solution of your problem is setting a flag with an idle timer, https://www.gnu.org/software/emacs/manual/html_node/elisp/Idle-Timers.html and checking/unsetting it with post-self-insert-...
user avatar
6 votes
Accepted

How to detect if `read-event` or `y-or-n-p` is running?

Best method seems to be found in eldoc that also displays temporary info in minibuffer: ;; Decide whether now is a good time to display a message. (defun eldoc-display-message-p () (and (eldoc-...
user avatar
6 votes

Translating strings to keypresses

Does (execute-kbd-macro (kbd "Hello")) work by any chance?
user avatar
  • 9,993
4 votes

How can I simulate an arbitary key event from Elisp?

Taken from this answer, you can use global-set-key like this (global-set-key (kbd "C-`") (kbd "<escape>")) Which will treat C-` as escape This does seem to have some problems though if the ...
user avatar
  • 391
4 votes
Accepted

How can I convert a C-key code into a 'normal' key code?

I think you want event-basic-type. E.g. (event-basic-type ?\C-;) returns ?;. If you want to only stop the control modifier but keep the other modifiers (e.g. the shift modifier), then you can try ...
user avatar
  • 25.2k
4 votes

Translating strings to keypresses

Okay, so my first answer has a number of shortcomings as detailed in its comments. execute-kbd-macro is a built-in function in C source code. (execute-kbd-macro MACRO &optional COUNT LOOPFUNC) ...
user avatar
  • 9,993
3 votes
Accepted

How to access mouse event coordinates? (conveniently)

There is a list of functions with the posn prefix to extract that information and more from mouse events. One caveat is that many of them require a start/end event: (let ((e (read-event))) (when (...
user avatar
  • 21k
3 votes
Accepted

How to get the string representation of a keymap event?

If a single key/character, just use function single-key-description. (single-key-description 19) ; => "C-s" If a list of characters, convert them to a string and use function key-description. (...
user avatar
  • 69.2k
3 votes
Accepted

How to use unread-command-events variable?

How can i achieve my goal? Should i take another approach? IIUC the problem arises from a misuse of nconc. The value of unread-command-events is often nil, in which case nconc will not alter it in-...
user avatar
  • 11.3k
3 votes

Making read-char input robust?

Ok: the answer really is just to use read-char-exclusive instead of read-char. It works perfectly. I wasn't working for me because of some problem in my configurations.
user avatar
  • 61
2 votes

How can I simulate an arbitary key event from Elisp?

After reading the suggestion from jch to use unread-command-events, I was able to hack together a solution that will do some of the things that I am looking for. (defun my-simulate-key-event (event &...
user avatar
  • 7,763
2 votes

Remap input event in all contexts

function-key-map and input-decode-map definitely apply in the middle of a key-sequence as well.
user avatar
  • 25.2k
2 votes
Accepted

elisp - detect line change event to trigger function

The answer to “I want to detect (…) event” is usually to use the appropriate hook. The manual has a list of standard hooks. There's no hook that triggers on a line change, so the next thing is to look ...
user avatar
1 vote
Accepted

`read-event`: How to cancel a mouse event once read

Whenever you click on the mouse, two events are generated. In this case down-mouse-1 and mouse-1. Your code consumes down-mouse-1, but leaves the second in the queue. If you consume both, the problem ...
user avatar
  • 5,735
1 vote

tooltip-functions not working?

Not exactly an answer, but a dirty workaround: give up using tooltip-functions and bind the function directly to mouse-movement, like this: (defun my-tooltip (event) (interactive "e") (...
user avatar
  • 161
1 vote

unread-command-events and batch mode

This is expected behavior, but you can circumvent this problem by let-binding executing-kbd-macro to t, which will convince the minibuffer commands to read from unread-command-events rather than from ...
user avatar
  • 25.2k
1 vote
Accepted

How to count the number of clocks of some given duration?

my-count-clocks in the following Elisp section defines a function that counts clocks in the current org-mode section. It does not descent into sub-sections. You can easily modify my-count-clocks to ...
user avatar
  • 29.9k
1 vote
Accepted

Wait for release a key to go on in a loop

Release of a keyboard key is not an event that is detected by Emacs. (But release of a mouse button is an event seen by Emacs.) Maybe change from reading and acting on a single character to acting on ...
user avatar
  • 69.2k
1 vote

How to detect if `read-event` or `y-or-n-p` is running?

For y-or-n-p, you could check the value of input-method-use-echo-area. It is turned on by read-key-sequence, which is indirectly used by y-or-n-p – and quite likely by various other commands you'd ...
user avatar
1 vote

Send input to multiple buffers simultaneously

If you have to stay in emacs, then the two options I've tried are: (process-send-string "my-vm-1" "command to vm") where I'd change my-vm-1 to my-vm-2 and so on in a macro. The other option I use ...
user avatar
  • 5,325
1 vote
Accepted

Help with my double tap key-binding function

Based on phils' comment, this is what I needed to do to make it work: (setq doubletap-flag nil) (defun doubletap (doubletap-key1 doubletap-key2 doubletap-wait doubletap-function) (setq doubletap-...
user avatar
  • 768
1 vote
Accepted

How to move point with mousepointer on shift-click in emacs

You need to manually move the point in your function like this: (defun mouse-test (event) "Move the point to the clicked position and message the thing at point." (interactive "e") (let ((es (...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible