all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to know if a key is pressed without getting it?
@ 2010-09-26 15:02 Vinicius Jose Latorre
  2010-09-26 15:53 ` Óscar Fuentes
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Vinicius Jose Latorre @ 2010-09-26 15:02 UTC (permalink / raw)
  To: GNU Emacs (devel)

Hi,


Does anyone know how to know if a key is pressed without getting it in 
Emacs (like kbhit like function)?

I tried to get some information from Emacs Lisp manual but I didn't find 
anything that could help me.


Thanks,


Vinicius




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 15:02 How to know if a key is pressed without getting it? Vinicius Jose Latorre
@ 2010-09-26 15:53 ` Óscar Fuentes
  2010-09-26 22:15 ` Stefan Monnier
  2010-09-26 22:46 ` Andreas Schwab
  2 siblings, 0 replies; 13+ messages in thread
From: Óscar Fuentes @ 2010-09-26 15:53 UTC (permalink / raw)
  To: emacs-devel

Hello Vinicius.

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

> Does anyone know how to know if a key is pressed without getting it in
> Emacs (like kbhit like function)?

I'm sure others wil provide the exact method, but until that maybe you
can get some benefit from reading this:

http://www.gnu.org/software/emacs/manual/html_node/elisp/Event-Input-Misc.html#Event-Input-Misc

"This section describes how to “peek ahead” at events without using them
up..."

[snip]




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 15:02 How to know if a key is pressed without getting it? Vinicius Jose Latorre
  2010-09-26 15:53 ` Óscar Fuentes
@ 2010-09-26 22:15 ` Stefan Monnier
  2010-09-26 22:41   ` Vinicius Jose Latorre
  2010-09-26 22:46 ` Andreas Schwab
  2 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2010-09-26 22:15 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: GNU Emacs (devel)

> Does anyone know how to know if a key is pressed without getting it in Emacs
> (like kbhit like function)?

I don't know kbhit, so I'm not sure what you're asking.
But it sounds like something that can't be done right, so if you give us
a more general description of the problem at hand, we may be able to
suggest a different solution.


        Stefan



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 22:15 ` Stefan Monnier
@ 2010-09-26 22:41   ` Vinicius Jose Latorre
  2010-09-26 23:11     ` Stefan Monnier
  2010-09-27  1:20     ` Ted Zlatanov
  0 siblings, 2 replies; 13+ messages in thread
From: Vinicius Jose Latorre @ 2010-09-26 22:41 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: GNU Emacs (devel)


>> Does anyone know how to know if a key is pressed without getting it in Emacs
>> (like kbhit like function)?
>>      
> I don't know kbhit, so I'm not sure what you're asking.
> But it sounds like something that can't be done right, so if you give us
> a more general description of the problem at hand, we may be able to
> suggest a different solution.

The kbhit (keyboard hit) is a function that returns true if a key was 
pressed.

This function is used in Windows but there is also kbhit implementation 
for GNU/Linux.

The problem that I'm trying to fix is an specific slow down in 
whitespace when user holds space key pressed at end of a line. The slow 
down is caused by whitespace which call jit-font-lock-refontify each 
time the user press a space at end of line. If it is possible to detect 
that user is still pressing a key, I could fix the slow down by not 
calling jit-font-lock-refontify while user is pressing a key.

BTW, calling jit-font-lock-refontify in Emacs 22 didn't cause any slow 
down, but this happens in Emacs 23 and 24. Probably 
jit-font-lock-refontify or some part of font-lock was modified in Emacs 23.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 15:02 How to know if a key is pressed without getting it? Vinicius Jose Latorre
  2010-09-26 15:53 ` Óscar Fuentes
  2010-09-26 22:15 ` Stefan Monnier
@ 2010-09-26 22:46 ` Andreas Schwab
  2010-09-26 23:21   ` Vinicius Jose Latorre
  2 siblings, 1 reply; 13+ messages in thread
From: Andreas Schwab @ 2010-09-26 22:46 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: GNU Emacs (devel)

Vinicius Jose Latorre <viniciusjl@ig.com.br> writes:

> Does anyone know how to know if a key is pressed without getting it in
> Emacs (like kbhit like function)?

input-pending-p is a built-in function in `C source code'.

(input-pending-p)

Return t if command input is currently available with no wait.
Actually, the value is nil only if we can be sure that no input is available;
if there is a doubt, the value is t.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 22:41   ` Vinicius Jose Latorre
@ 2010-09-26 23:11     ` Stefan Monnier
  2010-09-26 23:25       ` Lennart Borgman
  2010-09-27  1:08       ` Vinicius Jose Latorre
  2010-09-27  1:20     ` Ted Zlatanov
  1 sibling, 2 replies; 13+ messages in thread
From: Stefan Monnier @ 2010-09-26 23:11 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: GNU Emacs (devel)

> The problem that I'm trying to fix is an specific slow down in whitespace
> when user holds space key pressed at end of a line. The slow down is caused
> by whitespace which call jit-font-lock-refontify each time the user press
> a space at end of line. If it is possible to detect that user is still
> pressing a key, I could fix the slow down by not calling
> jit-font-lock-refontify while user is pressing a key.

Calling it without any BEG and END args is a bad idea for something that
can run at each key press.

You could try to check input-pending-p, but note that the pending input
may end up not running any command, so you may end up with a display in
an incorrect state for an unlimited amount of time, since the next
post-command-hook may not be run for the same unlimited amount of time.

> BTW, calling jit-font-lock-refontify in Emacs 22 didn't cause any slow down,
> but this happens in Emacs 23 and 24. Probably jit-font-lock-refontify or
> some part of font-lock was modified in Emacs 23.

I can't think of what that slowdown may come from, so it might be
worth investigating.


        Stefan



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 22:46 ` Andreas Schwab
@ 2010-09-26 23:21   ` Vinicius Jose Latorre
  2010-09-26 23:36     ` Stefan Monnier
  0 siblings, 1 reply; 13+ messages in thread
From: Vinicius Jose Latorre @ 2010-09-26 23:21 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: GNU Emacs (devel)


>> Does anyone know how to know if a key is pressed without getting it in
>> Emacs (like kbhit like function)?
>>      
> input-pending-p is a built-in function in `C source code'.
>
> (input-pending-p)
>
> Return t if command input is currently available with no wait.
> Actually, the value is nil only if we can be sure that no input is available;
> if there is a doubt, the value is t.
>    

Well, Óscar Fuentes also sent me an email about input-pending-p.

I defined the following function using input-pending-p:

(defun test-input ()
   (read-char "char: ")
   (while (input-pending-p)
     (insert (format "{%S}" unread-command-events)))
   (message "{%S} END" last-input-char))

Execute the function above via: M-: (test-input) RET

Then hold space key.

The only message returned is "{32} END" followed by all spaces that I 
pressed (except the first one).

So, input-pending-p does not work as kbhit function.

I'm thinking that this function is not implemented in Emacs.



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 23:11     ` Stefan Monnier
@ 2010-09-26 23:25       ` Lennart Borgman
  2010-09-27  1:08       ` Vinicius Jose Latorre
  1 sibling, 0 replies; 13+ messages in thread
From: Lennart Borgman @ 2010-09-26 23:25 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: GNU Emacs (devel)

On Mon, Sep 27, 2010 at 1:11 AM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
>> The problem that I'm trying to fix is an specific slow down in whitespace
>> when user holds space key pressed at end of a line. The slow down is caused
>> by whitespace which call jit-font-lock-refontify each time the user press
>> a space at end of line. If it is possible to detect that user is still
>> pressing a key, I could fix the slow down by not calling
>> jit-font-lock-refontify while user is pressing a key.
>
> Calling it without any BEG and END args is a bad idea for something that
> can run at each key press.
>
> You could try to check input-pending-p, but note that the pending input
> may end up not running any command, so you may end up with a display in
> an incorrect state for an unlimited amount of time, since the next
> post-command-hook may not be run for the same unlimited amount of time.

Running it in an idle timer?



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 23:21   ` Vinicius Jose Latorre
@ 2010-09-26 23:36     ` Stefan Monnier
  2010-09-27  0:29       ` Vinicius Jose Latorre
  0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2010-09-26 23:36 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: Andreas Schwab, GNU Emacs (devel)

> I defined the following function using input-pending-p:

> (defun test-input ()
>   (read-char "char: ")
>   (while (input-pending-p)
>     (insert (format "{%S}" unread-command-events)))
>   (message "{%S} END" last-input-char))

> Execute the function above via: M-: (test-input) RET

> Then hold space key.

> The only message returned is "{32} END" followed by all spaces that
> I pressed (except the first one).

This probably depends on how quickly the read-char returns and the
input-pending-p gets run, but it seems reasonable to expect it to be run
in (much) less time than your repeat rate, so when the input-pending-p
is run, there is indeed no input pending yet.


        Stefan



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 23:36     ` Stefan Monnier
@ 2010-09-27  0:29       ` Vinicius Jose Latorre
  0 siblings, 0 replies; 13+ messages in thread
From: Vinicius Jose Latorre @ 2010-09-27  0:29 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Andreas Schwab, GNU Emacs (devel)


>> I defined the following function using input-pending-p:
>>      
>> (defun test-input ()
>>    (read-char "char: ")
>>    (while (input-pending-p)
>>      (insert (format "{%S}" unread-command-events)))
>>    (message "{%S} END" last-input-char))
>>      
>> Execute the function above via: M-: (test-input) RET
>>      
>> Then hold space key.
>>      
>> The only message returned is "{32} END" followed by all spaces that
>> I pressed (except the first one).
>>      
> This probably depends on how quickly the read-char returns and the
> input-pending-p gets run, but it seems reasonable to expect it to be run
> in (much) less time than your repeat rate, so when the input-pending-p
> is run, there is indeed no input pending yet.
>    

Ok, you're right, I redefined test-input:

(defun test-input ()
   (read-char "char: ")
   (sleep-for 0.3)
   (while (input-pending-p)
     (insert (format "{%S}" unread-command-events)))
   (message "{%S} END" last-input-char))

Now, input-pending-p is working.

So, input-pending-p will not work to fix the problem, because the event 
(key being holded) happens after the detection (input-pending-p).




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 23:11     ` Stefan Monnier
  2010-09-26 23:25       ` Lennart Borgman
@ 2010-09-27  1:08       ` Vinicius Jose Latorre
  2010-09-27 20:30         ` Stefan Monnier
  1 sibling, 1 reply; 13+ messages in thread
From: Vinicius Jose Latorre @ 2010-09-27  1:08 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: GNU Emacs (devel)

>
>> The problem that I'm trying to fix is an specific slow down in whitespace
>> when user holds space key pressed at end of a line. The slow down is caused
>> by whitespace which call jit-font-lock-refontify each time the user press
>> a space at end of line. If it is possible to detect that user is still
>> pressing a key, I could fix the slow down by not calling
>> jit-font-lock-refontify while user is pressing a key.
>>      
> Calling it without any BEG and END args is a bad idea for something that
> can run at each key press.
>    

Despite jit-font-lock-refontify args, this was not a problem in Emacs 22.

In Emacs 22, jit-font-lock-refontify was called at each end of line with 
or without trailing spaces.

I had to modify this because since Emacs 23, calling 
jit-font-lock-refontify causes a slow down in whitespace-mode.

> You could try to check input-pending-p, but note that the pending input
> may end up not running any command, so you may end up with a display in
> an incorrect state for an unlimited amount of time, since the next
> post-command-hook may not be run for the same unlimited amount of time.
>    

Yes, you're right.


>> BTW, calling jit-font-lock-refontify in Emacs 22 didn't cause any slow down,
>> but this happens in Emacs 23 and 24. Probably jit-font-lock-refontify or
>> some part of font-lock was modified in Emacs 23.
>>      
> I can't think of what that slowdown may come from, so it might be
> worth investigating.

I agree.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-26 22:41   ` Vinicius Jose Latorre
  2010-09-26 23:11     ` Stefan Monnier
@ 2010-09-27  1:20     ` Ted Zlatanov
  1 sibling, 0 replies; 13+ messages in thread
From: Ted Zlatanov @ 2010-09-27  1:20 UTC (permalink / raw)
  To: emacs-devel

On Sun, 26 Sep 2010 19:41:29 -0300 Vinicius Jose Latorre <viniciusjl@ig.com.br> wrote: 

VJL> The problem that I'm trying to fix is an specific slow down in
VJL> whitespace when user holds space key pressed at end of a line. The
VJL> slow down is caused by whitespace which call jit-font-lock-refontify
VJL> each time the user press a space at end of line. If it is possible to
VJL> detect that user is still pressing a key, I could fix the slow down by
VJL> not calling jit-font-lock-refontify while user is pressing a key.

I wonder if this could be handled with an Emacs facility, e.g.:

; triggers when it's held less than 30 secs
(global-set-key [(hold < 30.0) (shift f4)] 'something)

; triggers when it's held 30 secs or more
(global-set-key [(hold >= 30.0) (shift f4)] 'something-else)

Just an idea...  Forgive the noise if it's nonsense :)

Ted




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: How to know if a key is pressed without getting it?
  2010-09-27  1:08       ` Vinicius Jose Latorre
@ 2010-09-27 20:30         ` Stefan Monnier
  0 siblings, 0 replies; 13+ messages in thread
From: Stefan Monnier @ 2010-09-27 20:30 UTC (permalink / raw)
  To: Vinicius Jose Latorre; +Cc: GNU Emacs (devel)

> I had to modify this because since Emacs 23, calling jit-font-lock-refontify
> causes a slow down in whitespace-mode.
[...]
>> I can't think of what that slowdown may come from, so it might be
>> worth investigating.
> I agree.

For one, the time it takes to refontify should depend heavily on the
major mode, so you may want to try it out in various major modes.


        Stefan



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2010-09-27 20:30 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-26 15:02 How to know if a key is pressed without getting it? Vinicius Jose Latorre
2010-09-26 15:53 ` Óscar Fuentes
2010-09-26 22:15 ` Stefan Monnier
2010-09-26 22:41   ` Vinicius Jose Latorre
2010-09-26 23:11     ` Stefan Monnier
2010-09-26 23:25       ` Lennart Borgman
2010-09-27  1:08       ` Vinicius Jose Latorre
2010-09-27 20:30         ` Stefan Monnier
2010-09-27  1:20     ` Ted Zlatanov
2010-09-26 22:46 ` Andreas Schwab
2010-09-26 23:21   ` Vinicius Jose Latorre
2010-09-26 23:36     ` Stefan Monnier
2010-09-27  0:29       ` Vinicius Jose Latorre

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.