Vim Tips Wiki
(Adjust previous/next navigation)
(Remove html character entities)
Line 14: Line 14:
 
Someone once posted a patch to add a new event called GetChar to receive an event for every keypress. This trick is not as powerful and flexible as that, but it can be very useful for a plugin, and is supported in Vim 7.0 with no patches.
 
Someone once posted a patch to add a new event called GetChar to receive an event for every keypress. This trick is not as powerful and flexible as that, but it can be very useful for a plugin, and is supported in Vim 7.0 with no patches.
   
Often there are questions on how to capture every key press from a user. The answer is that you can't, unless you map all keys. But even if you map all keys, it is not flexible enough. Here is a trick with recursive &lt;expr&gt; maps and <tt>getchar()</tt> to have all keys pass through your function. You can do whatever you want with the keys, swallow them or pass them to Vim.
+
Often there are questions on how to capture every key press from a user. The answer is that you can't, unless you map all keys. But even if you map all keys, it is not flexible enough. Here is a trick with recursive <expr> maps and <tt>getchar()</tt> to have all keys pass through your function. You can do whatever you want with the keys, swallow them or pass them to Vim.
   
Here is a demo that shows how to use it in insert mode. What the function does is to double every key you press, except &lt;Esc&gt; and &lt;C-C&gt;, when it breaks the loop.
+
Here is a demo that shows how to use it in insert mode. What the function does is to double every key you press, except <Esc> and <C-C>, when it breaks the loop.
   
 
<pre>
 
<pre>
imap &lt;buffer&gt; &lt;silent&gt; &lt;expr&gt; &lt;F12&gt; Double("\&lt;F12&gt;")
+
imap <buffer> <silent> <expr> <F12> Double("\<F12>")
 
function! Double(mymap)
 
function! Double(mymap)
 
try
 
try
 
let char = getchar()
 
let char = getchar()
 
catch /^Vim:Interrupt$/
 
catch /^Vim:Interrupt$/
let char = "\&lt;Esc&gt;"
+
let char = "\<Esc>"
 
endtry
 
endtry
 
"exec BPBreakIf(char == 32, 1)
 
"exec BPBreakIf(char == 32, 1)
Line 30: Line 30:
 
let char = nr2char(char)
 
let char = nr2char(char)
 
endif " It is the ascii code.
 
endif " It is the ascii code.
if char == "\&lt;Esc&gt;"
+
if char == "\<Esc>"
 
return ''
 
return ''
 
endif
 
endif
 
redraw
 
redraw
return char.char."\&lt;C-R&gt;=Redraw()\&lt;CR&gt;".a:mymap
+
return char.char."\<C-R>=Redraw()\<CR>".a:mymap
 
endfunction
 
endfunction
   
Line 43: Line 43:
 
</pre>
 
</pre>
   
You can do almost anything that you can do normally in an insert mode, press &lt;BS&gt;, &lt;C-U&gt; etc.
+
You can do almost anything that you can do normally in an insert mode, press <BS>, <C-U> etc.
   
 
==Comments==
 
==Comments==
 
----
 

Revision as of 00:17, 30 September 2008

Tip 1363 Printable Monobook Previous Next

created October 19, 2006 · complexity advanced · author hari_vim · version 


Someone once posted a patch to add a new event called GetChar to receive an event for every keypress. This trick is not as powerful and flexible as that, but it can be very useful for a plugin, and is supported in Vim 7.0 with no patches.

Often there are questions on how to capture every key press from a user. The answer is that you can't, unless you map all keys. But even if you map all keys, it is not flexible enough. Here is a trick with recursive <expr> maps and getchar() to have all keys pass through your function. You can do whatever you want with the keys, swallow them or pass them to Vim.

Here is a demo that shows how to use it in insert mode. What the function does is to double every key you press, except <Esc> and <C-C>, when it breaks the loop.

imap <buffer> <silent> <expr> <F12> Double("\<F12>")
function! Double(mymap)
  try
    let char = getchar()
  catch /^Vim:Interrupt$/
    let char = "\<Esc>"
  endtry
  "exec BPBreakIf(char == 32, 1)
  if char == '^\d\+$' || type(char) == 0
    let char = nr2char(char)
  endif " It is the ascii code.
  if char == "\<Esc>"
    return ''
  endif
  redraw
  return char.char."\<C-R>=Redraw()\<CR>".a:mymap
endfunction

function! Redraw()
  redraw
  return ''
endfunction

You can do almost anything that you can do normally in an insert mode, press <BS>, <C-U> etc.

Comments