7

मैं सूक्ति-खोल GJS उपयोग करने के लिए सरल जीटीके आवेदन बनाने के लिए कोशिश कर रहा हूँ काम नहीं करता।GJS: Gtk.TextView कुंजी प्रेस घटना

इसकी खिड़की Gtk.TextView केवल होता है और मैं जब उपयोगकर्ता लिख ​​रहा है घटनाओं पर कार्रवाई करना चाहते हैं।

#!/usr/bin/gjs 

var Gtk = imports.gi.Gtk; 

function MainWindow() { 
    this._init(); 
} 

MainWindow.prototype = { 
    _init: function() { 
     this.window = new Gtk.Window ({ 
      title: "Just Calculator", 
      window_position: Gtk.WindowPosition.CENTER, 
      default_height: 400, 
      default_width: 440, 
     }); 

     //this.window.show(); 
     this.window.connect ("hide", Gtk.main_quit); 
     this.window.connect ("delete-event", function() { 
      Gtk.main_quit(); 
      return true; 
     }); 

     this.textbox = new Gtk.TextView(); 
     this.textbox.connect('key-press-event', this._keyPress); 

     var sw = new Gtk.ScrolledWindow ({shadow_type:Gtk.ShadowType.IN}); 
     sw.add (this.textbox); 
     this.window.add(sw); 

     this.window.show_all(); 
    }, 

    _keyPress: function(textview, event) { 
     print(event, event.type, event.keyval); 
     textview.buffer.text = 'ok'; 
     return true; 
    } 
} 

Gtk.init (null, null); 
var window = new MainWindow(); 
Gtk.main(); 

यह आम तौर पर काम करता है, लेकिन मैं event.keyval नहीं पढ़ सकते हैं: कंसोल आउटपुट "अनिर्धारित" है:

यहाँ मेरी कोड है

[union instance proxy GIName:Gdk.Event [email protected] [email protected]] undefined undefined 

कोई मुझे बता सकते हैं कि मैं क्या कर रहा हूँ गलत? धन्यवाद!

+0

पर इसे ठीक करने का प्रबंधन किया? – gosukiwi

+0

इसे आजमाएं: '_keyPress: फ़ंक्शन (स्वयं, टेक्स्टव्यू, ईवेंट)' – Gonzalo

उत्तर

0

Gdk.Event गुण type या keyval शामिल नहीं है और इसलिए वे undefined हैं। यह लंबे समय से उस के लिए चारों ओर नहीं किया गया है, लेकिन अब वहाँ https://people.gnome.org/~gcampagna/docs पर दस्तावेज़ GJS को GObject आत्मनिरीक्षण बाइंडिंग के लिए उपलब्ध है।

अपना प्रिंट आउट से आप देखते हैं कि event एक Gdk.Event है और उस के लिए प्रलेखन https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.Event.html पर है। वहां आप देख सकते हैं कि get_event_type और get_keyval फ़ंक्शन हैं। पहले रिटर्न एक Gdk.EventType (https://people.gnome.org/~gcampagna/docs/Gdk-3.0/Gdk.EventType.html) और बाद एक सरणी जहां दूसरा तत्व दबाया कुंजी के लिए सांख्यिक कोड होता है। आप KEY_ से शुरू होने वाले क्लटर में संख्यात्मक कुंजी की स्थिरता की तुलना कर सकते हैं।

उदाहरण के लिए कुछ समझदार उत्पादन प्राप्त करने के लिए

print(event, 
     event.get_event_type() === Gdk.EventType.KEY_PRESS, 
     event.get_keyval()[1] === Clutter.KEY_Escape); 

करने के लिए अपने कोड

var Gdk = imports.gi.Gdk; 
var Clutter = imports.gi.Clutter; 

के शीर्ष करने के लिए कुछ आयात जोड़ सकते हैं और प्रवेश लाइन बदल जाते हैं।

संबंधित मुद्दे