ruby-****@sourc*****
ruby-****@sourc*****
2012年 10月 25日 (木) 06:04:15 JST
------------------------- REMOTE_ADDR = 184.145.49.94 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-mnstbs-popup ------------------------- @@ -284,3 +284,44 @@ It is possible to manually create keyboard accelerators with Gtk::AccelMap, but in most cases the Gtk::Widget#add_accelerator#add_accelerator will provide all the necessary functionality. + + + + +=== Accelerators can also be used in programs without menus + +{{image_right("accelerator-keys-2.png")}} + +All accelerators, regardless of whether they are associated with menus or not use global Gtk::AccelMap object which defines a list entries in this table in the form of mappings between a unique accelerator name (in the Gtk::AccelMap known as the ((*'accel_path')*))and the actual key combinations. These 'accel_paths' eventually have to be mapped to actions either with the help of 'Gtk::AccelGroup#connect(accel_key, accel_mods, accel_flags, closure)', 'Gtk::AccelGroup#connect(accel_key, accel_mods, accel_flags) {...}', or 'Gtk::AccelGroup#connect(accel_path, closure)', or 'Gtk::AccelGroup#connect(accel_path) {...}'. + +Following program example will make things more clear: + +((*custom-accelerators*)) + + #!/usr/bin/env ruby + + require 'gtk2' + + ag = Gtk::AccelGroup.new + + ag.connect(Gdk::Keyval::GDK_Z, Gdk::Window::CONTROL_MASK, + Gtk::ACCEL_VISIBLE) { + p "You've presswd Ctrl+Z." + } + + Gtk::AccelMap.add_entry("<AccelMap Demo>/test_a", Gdk::Keyval::GDK_A, 0) + Gtk::AccelMap.add_entry("<AccelMap Demo>/test_b", Gdk::Keyval::GDK_B, 0) + Gtk::AccelMap.add_entry("<AccelMap Demo>/test ctrl+sh+c", Gdk::Keyval::GDK_C, + Gdk::Window::CONTROL_MASK|Gdk::Window::SHIFT_MASK) + + ag.connect("<AccelMap Demo>/test_a") { p "Hello with (A)" } + ag.connect("<AccelMap Demo>/test_b") { p "Hello (B)" } + ag.connect("<AccelMap Demo>/test ctrl+sh+c") { p "Hello World, with (Ctrl+Shift+C)" } + + win = Gtk::Window.new("Custom Acceleratots") + win.add(Gtk::Label.new("Press keys:\n\tA, B, Ctrl+Shift+C,\n\tor Ctrl+Z")) + win.resizable = true + win.set_size_request(250, -1) + win.signal_connect("destroy") {Gtk.main_quit} + win.add_accel_group(ag).show_all + Gtk.main