[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Console Application Keyboard event catch

marble.zhong

7/3/2007 4:17:00 AM



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

-----Original Message-----
From: Jano Svitok [mailto:jan.svitok@gmail.com]
Sent: Tuesday, July 03, 2007 5:35 AM
To: ruby-talk@ruby-lang.org
Subject: Re: Console Application Keyboard event catch

On 7/2/07, marble.zhong@oocl.com <marble.zhong@oocl.com> wrote:
>
>
>
> Dear All,
>
> While doing the console application with Ruby, met below questions,
> please help, thanks in advance,
>
> 1. How to catch the Keyboard event, such as, how about the
> [TAB] key pressed, [CTRL] key and so on;
> 2. How to backdisplay the string? Such as, when you type [cd]
> command at the windows command mode, and press [TAB] it will help to
> complete the directory name, such as [abc] directory, once you press
> again, it will delete the [abc] directory and display another one,
> such as [efg].
>
> I try search from the google, but no result, please help thanks.

1. TAB is character #9, Ctrl+A is (IIRC) 0, Ctrl+B 1, etc. If you want
to catch all the combinations, you need to use special api, i.e. getch
from msvcrt, or curses.

2. BS (backspace, Ctrl+H) moves cursor one char left. CR (carriege
return, 13) moves cursor to the start of the line (you need to overwrite
everything you have written before, and it's possible that a screen will
scroll up, and LF (line feed) will be inserted by ruby)

3. have a look at readline library, it may do what you are trying to
achieve.

J.








IMPORTANT NOTICE
Email from OOCL is confidential and may be legally privileged. If it is not intended for you, please delete it immediately unread. The internet cannot guarantee that this communication is free of viruses, interception or interference and anyone who communicates with us by email is taken to accept the risks in doing so. Without limitation, OOCL and its affiliates accept no liability whatsoever and howsoever arising in connection with the use of this email. Under no circumstances shall this email constitute a binding agreement to carry or for provision of carriage services by OOCL, which is subject to the availability of carrier's equipment and vessels and the terms and conditions of OOCL's standard bill of lading which is also available at http://ww....

1 Answer

Jano Svitok

7/3/2007 10:52:00 PM

0

On 7/3/07, marble.zhong@oocl.com <marble.zhong@oocl.com> 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.