The Accidental Rubyist

invalid byte sequence in UTF-8

Hotkeys with Labels (and other buttons)

leave a comment »

We may associate a label with any field/widget with the label_for method. If a mnemonic has been defined for the label, it will register the ALT-key, to transfer keyboard focus to that field.

label hotkeys will transfer focus to buddy fields

label hotkeys will transfer focus to buddy fields

If that field is a button (responds to fire), its fire will be called.

e.g. we had created some fields in a loop. Now we add mnemonics to them.


      mnemonics = %w[ n l r p]
      %w[ name line regex password].each_with_index do |w,i|
        field = Field.new @form do
          name   w
          row  r
          col  c
          display_length  30
          set_buffer "abcd "
          set_label Label.new @form, {'text' => w, 'mnemonic'=> mnemonics[i]}
        end
        r += 1
      end
 

We had created a label for 2 radio buttons. Now we can associate the label with one radio button.


      colorlabel = Label.new @form, {'text' => "Select a color:", "row" => 21, "col" => 22, "color"=>"cyan", "mnemonic" => 'S'}
      # after declaring radio1
      colorlabel.label_for radio1
 

We had used the underline property to create hotkeys for buttons. Now we use the same property to create hotkeys for radiobuttons, checkboxes and togglebuttons as can be seen in the screenshot below.

Button hotkeys execute the fire method

Button hotkeys execute the fire method

The underline property was taken from Tk, however, i think its a dirty hack. A mnemonic is a better idea. I am considering phasing out underline in favor of mnemonic.

In other news, combo selection behaviour has been improved. Check the tweets on the sidebar for small changes/enhancements.

Advertisements

Written by totalrecall

December 21, 2008 at 1:13 am

Posted in ncurses, rbcurse, ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: