|
From: | Julio C. Neves |
Subject: | Re: [shell-script] "trap" nas setas do teclado |
Date: | Fri, 4 Mar 2016 22:07:24 -0300 |
o trap no CTRL+\ é exatamente oq eu precisava.
Como consigo descobrir a "lista" de sinais gerados com keystrokes? Tem como?On Fri, Mar 4, 2016 at 5:15 PM 'Julio C. Neves' address@hidden [shell-script] <address@hidden> wrote:Grande Casanova,Em qq shell
Le ()
{
stty $OLD_STTY
stty cbreak -echo
look=`dd if=/dev/tty bs=3 count=1 2>/dev/null`
stty $OLD_STTY} # Com bs=1 funciona para caracteres adaptei e não testei bs=3 para setasNo bash:
read -n2; read -n1 Letra # já que as setas levam 3 char. Letra terá uma Letra que indicará a direção da setaOutra dica:
Será que no teu caso vc poderia fazer um <CTRL>+\, que gera o sinal 3 e na trap desse sinal seria uma chamada de uma função com bind?Creio que para fazer isso vc precise declarar a função com um export -f funcao.Algo mais ou menos assim:function baind
{bind ...
...
}export -f baindtrap "bash -c baind" 3Tomara que uma dessas sirva, pq senão não sei mais o que chutar!Bom fim de semana a todos!
OBS: Pessoal, em abril darei um treinamento no RJ em 2 fins de semana (32 horas) por um preço ridiculamente baixo, porém será no *seu* notebook, *não* darei livros (mas consigo 30% de desconto na compra) e *sem* coffee break. Se alguém estiver a fim, me mande um e-mail em pvt que passo a ementa, explico como, onde e quando será o treinamento e como consegui chegar a esse preço ridículo.2016-03-04 15:54 GMT-03:00 Alfredo Casanova address@hidden [shell-script] <address@hidden>:Como eu disse no meu e-mail, já tinha visto o comando bind. O problema é que ele entra na fila de execução de comandos, e não é executado independente do que está rodando.
Veja se isso ajuda...
http://unix.stackexchange.com/questions/89622/how-to-execute-a-script-in-shell-when-a-shortcut-key-is-pressedYou can use the builtin command,bind
to map a keyboard shortcut so that it executes a command/shell script.
Example
Say we want to run the command,pwd
, when we press the F12 key.
Now when I press F12 at my prompt,$ bind '"\e[24~":"pwd\n"'
$
:
$ pwd /home/saml
Determining keyboard shortcuts
You can use the following technique to determine the escape code for a given keyboard shortcut. First press the keys Ctrl + M, then press the key that you want the code for.
Example
Pressing Ctrl + M + F12 in a terminal window returns this:
This output can be interpreted as follows,$ ^[[24~
^[
is the Esc key. So when we want to specify this particular key using thebind
command we need to use a\e
to denote the Esc key followed by everything else from above. So thebind
command looks like this:
$ bind '"\e[24~":"....."'
Executing a command in the middle
You can also make use ofbind -x
to setup keyboard shortcuts that will run commands while you're in the middle of typing something at the prompt, and these commands' output will be displayed, but what ever you were typing at the prompt will remain intact.
NOTE: This method only works with keyboard shortcuts that output 1 character, so F12 won't work here.$ bind -x '"\eW":"..."'
Example
Let's alias the keyboard shortcut Alt + Shift + W.
Say I'm typing the command$ bind -x '"\eW":"who"'
finger
:
Now I hit the keyboard shortcut Alt + Shift + W:$ finger
What's going on issaml tty1 2013-09-01 11:01 (:0) saml pts/0 2013-09-01 11:03 (:0.0) saml pts/1 2013-09-01 11:05 (:0.0) saml pts/2 2013-09-01 11:05 (:0.0) saml pts/5 2013-09-03 22:45 (:0.0) $ finger
bind
is running the command defined,who
, taking its output and inserting it in front of the prompt. If you repeat it you'll see what's going on, here's output from me hitting it 2 times:
saml tty1 2013-09-01 11:01 (:0) saml pts/0 2013-09-01 11:03 (:0.0) saml pts/1 2013-09-01 11:05 (:0.0) saml pts/2 2013-09-01 11:05 (:0.0) saml pts/5 2013-09-03 22:45 (:0.0) saml tty1 2013-09-01 11:01 (:0) saml pts/0 2013-09-01 11:03 (:0.0) saml pts/1 2013-09-01 11:05 (:0.0) saml pts/2 2013-09-01 11:05 (:0.0) saml pts/5 2013-09-03 22:45 (:0.0) $ finger
Your problem
So one idea would be to use thebind -x
method above andcat
to display this text file at your prompt:
Now when I run commands I can see this file like so:$ bind -x '"\eW":"cat someinfo.txt"'
The output of fileThis is text from some multi-line file reminding me how to do some stuff $ finger
someinfo.txt
is being displayed above myfinger
command above.
References
To: address@hidden
From: address@hidden
Date: Thu, 3 Mar 2016 21:50:56 +0000
Subject: [shell-script] "trap" nas setas do teclado
Existe alguma forma de eu executar comandos quando pressionar as setas do teclado (ou alguma combinação de teclas, como alt+P) durante a execução de um script?achei o comando bind -x, mas ele só executa algo quando o comando anterior termina. Queria algo parecido com as interrupções com trap de control+c, control+z, mas em teclas específicas.
[Prev in Thread] | Current Thread | [Next in Thread] |