Ruby Readline - do something when a key combination is pressed

Hi,

When I use readline, I would like to run an action if a key combination
is pressed.

Alt + cursor key left

Alt + cursor key right

In other words, I want the cursor key to do something, but only when it
is pressed in combination.

If this is not possible in Readline, perhaps someone knows how else to
fetch user input, including ALL possible key combination?

But, the problem also is … I do NOT want to press ENTER.

The user should just be able to press:

alt + cursor_key_left

And this should trigger something.

Is that even possible at all?

Marc H. wrote in post #1020870:

Hi,

When I use readline, I would like to run an action if a key combination
is pressed.

Alt + cursor key left

Alt + cursor key right

In other words, I want the cursor key to do something, but only when it
is pressed in combination.

If this is not possible in Readline, perhaps someone knows how else to
fetch user input, including ALL possible key combination?

But, the problem also is … I do NOT want to press ENTER.

The user should just be able to press:

alt + cursor_key_left

And this should trigger something.

Is that even possible at all?

It’s possible but it’s not clear if you’re talking about invoking it in
irb or a unix shell. If in irb, you can invoke a ruby method as you’d
like if you put this in ~/.inputrc:

$if Ruby
“\C-x\C-p”: “toggle_object_complete\n”
$endif

It’s possible but it’s not clear if you’re talking about invoking it in
irb or a unix shell. If in irb, you can invoke a ruby method as you’d
like if you put this in ~/.inputrc:

$if Ruby
“\C-x\C-p”: “toggle_object_complete\n”
$endif

If it was a unix shell, what would be the solution?

$if Ruby
“\C-x\C-p”: “toggle_object_complete\n”
$endif

Hmm. This seems useful. Can someone explain how this works?

C-x and C-p is probably cursor x and cursor p, which then
triggers a function in IRB? Is this possible with cursor keys
as well?

If it was a unix shell, what would be the solution?

Yes, I am curious about that as well.

I want to add “directory browsing” to a pseudo-shell I am writing.

If the user presses alt+left key or alt+right I want this to
translate into “change directory one level down”.

Not sure how to get access to that though.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs