# Re: Console Application Keyboard event catch

**URL:** <https://www.ruby-forum.com/t/re-console-application-keyboard-event-catch/108403>\
**Category:** Ruby\
**Created:** [July 3, 2007, 6:17am UTC](https://www.ruby-forum.com/t/re-console-application-keyboard-event-catch/108403 "2007-07-03T06:17:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 3, 2007, 6:17am UTC](https://www.ruby-forum.com/t/re-console-application-keyboard-event-catch/108403/1 "2007-07-03T06:17:53Z")

</div>

Thanks Jano,

Could make a sample for it? Or give me the related API? Thanks.  
And, want to catch the event, not only catch the what user input after  
press [Enter], thanks.

Marble

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 4, 2007, 12:52am UTC](https://www.ruby-forum.com/t/re-console-application-keyboard-event-catch/108403/2 "2007-07-04T00:52:50Z")

</div>

On 7/3/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Jano,
> 
> Could make a sample for it? Or give me the related API? Thanks.  
> And, want to catch the event, not only catch the what user input after  
> press [Enter], thanks.

Hi,

this is how you can obtain pressed keys on windows:

require ‘Win32API’

module Windows  
module Keyboard  
Getch = Win32API.new(‘crtdll’, ‘\_getch’, ‘V’, ‘L’)  
Kbhit = Win32API.new(‘crtdll’, ‘\_kbhit’, ‘V’, ‘L’)

```
def getch
  Getch.call
end

def kbhit
  Kbhit.call
end

```

end  
end

and this is how you can use readline library to prompt and provide  
completion:

require ‘readline’  
Readline.completion\_proc = proc {|word| [‘array’, ‘of’, ‘possible’,  
‘completions’]}  
line = Readline.readline(“prompt\>”, true) # true means history on

J.
