PyGTK3 : capturing a string from keybord
I had some code written in python3 using GTK3. The program waited for some inputs from the keyboard. For instance, if I pressed 'q' the program printed a specific symbol, etc.
This code looks like
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'q':
self.textbuffer.insert_at_cursor('you've pressed q')
I want to execute a callback function, when the program captures a specific string, e.g. when I press say, 'q' and 'w' in parallel (I press 'w' without leaving the key 'q') then, the program executes a specific function.
I didn't find something in GTK3, except a method called Gdk.utf8_to_string_target(event.string) but I did not manage to use it correctly. For pressing two characters such as ctrl and 'h' it's easy by using something like
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if ctrl and event.keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n')
I checked a seemingly similar question Detect specific keypresses in GUI, but was asking about checking if a special key is pressed. Which is not my case.
As a minimal code we provide the following.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyval == Gdk.KEY_space:
self.textbuffer.insert_at_cursor(' ') #space
if keyname == 'q':
self.textbuffer.insert_at_cursor('you pressed q')
# etc..
if ctrl and keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n') #change line when ctrl + h is pressed
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
My question is, If I can have something like
#you press q and after w or w and after q or the same time you press q and w
if keyname == 'qw':
self.textbuffer.insert_at_cursor('you pressed qw')
Any ideas? Please, if it is possible provide some code to prove your point!
python-3.x gtk3 pygtk gdk
add a comment |
I had some code written in python3 using GTK3. The program waited for some inputs from the keyboard. For instance, if I pressed 'q' the program printed a specific symbol, etc.
This code looks like
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'q':
self.textbuffer.insert_at_cursor('you've pressed q')
I want to execute a callback function, when the program captures a specific string, e.g. when I press say, 'q' and 'w' in parallel (I press 'w' without leaving the key 'q') then, the program executes a specific function.
I didn't find something in GTK3, except a method called Gdk.utf8_to_string_target(event.string) but I did not manage to use it correctly. For pressing two characters such as ctrl and 'h' it's easy by using something like
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if ctrl and event.keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n')
I checked a seemingly similar question Detect specific keypresses in GUI, but was asking about checking if a special key is pressed. Which is not my case.
As a minimal code we provide the following.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyval == Gdk.KEY_space:
self.textbuffer.insert_at_cursor(' ') #space
if keyname == 'q':
self.textbuffer.insert_at_cursor('you pressed q')
# etc..
if ctrl and keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n') #change line when ctrl + h is pressed
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
My question is, If I can have something like
#you press q and after w or w and after q or the same time you press q and w
if keyname == 'qw':
self.textbuffer.insert_at_cursor('you pressed qw')
Any ideas? Please, if it is possible provide some code to prove your point!
python-3.x gtk3 pygtk gdk
add a comment |
I had some code written in python3 using GTK3. The program waited for some inputs from the keyboard. For instance, if I pressed 'q' the program printed a specific symbol, etc.
This code looks like
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'q':
self.textbuffer.insert_at_cursor('you've pressed q')
I want to execute a callback function, when the program captures a specific string, e.g. when I press say, 'q' and 'w' in parallel (I press 'w' without leaving the key 'q') then, the program executes a specific function.
I didn't find something in GTK3, except a method called Gdk.utf8_to_string_target(event.string) but I did not manage to use it correctly. For pressing two characters such as ctrl and 'h' it's easy by using something like
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if ctrl and event.keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n')
I checked a seemingly similar question Detect specific keypresses in GUI, but was asking about checking if a special key is pressed. Which is not my case.
As a minimal code we provide the following.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyval == Gdk.KEY_space:
self.textbuffer.insert_at_cursor(' ') #space
if keyname == 'q':
self.textbuffer.insert_at_cursor('you pressed q')
# etc..
if ctrl and keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n') #change line when ctrl + h is pressed
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
My question is, If I can have something like
#you press q and after w or w and after q or the same time you press q and w
if keyname == 'qw':
self.textbuffer.insert_at_cursor('you pressed qw')
Any ideas? Please, if it is possible provide some code to prove your point!
python-3.x gtk3 pygtk gdk
I had some code written in python3 using GTK3. The program waited for some inputs from the keyboard. For instance, if I pressed 'q' the program printed a specific symbol, etc.
This code looks like
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'q':
self.textbuffer.insert_at_cursor('you've pressed q')
I want to execute a callback function, when the program captures a specific string, e.g. when I press say, 'q' and 'w' in parallel (I press 'w' without leaving the key 'q') then, the program executes a specific function.
I didn't find something in GTK3, except a method called Gdk.utf8_to_string_target(event.string) but I did not manage to use it correctly. For pressing two characters such as ctrl and 'h' it's easy by using something like
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if ctrl and event.keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n')
I checked a seemingly similar question Detect specific keypresses in GUI, but was asking about checking if a special key is pressed. Which is not my case.
As a minimal code we provide the following.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyval == Gdk.KEY_space:
self.textbuffer.insert_at_cursor(' ') #space
if keyname == 'q':
self.textbuffer.insert_at_cursor('you pressed q')
# etc..
if ctrl and keyval == Gdk.KEY_h:
self.textbuffer.insert_at_cursor('n') #change line when ctrl + h is pressed
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
My question is, If I can have something like
#you press q and after w or w and after q or the same time you press q and w
if keyname == 'qw':
self.textbuffer.insert_at_cursor('you pressed qw')
Any ideas? Please, if it is possible provide some code to prove your point!
python-3.x gtk3 pygtk gdk
python-3.x gtk3 pygtk gdk
edited Dec 31 '18 at 16:19
asked Dec 28 '18 at 0:35
111
1096
1096
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
there is a function(in gtk) which is something similar to your needs..
https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html#gtk-accel-group-connect
it connects a key and a key-mod (https://github.com/bstpierre/gtk-examples/blob/master/c/accel.c)
if you really want to capture a string then you could try recreating the function given in gtk_accel_group
the source can be found in git (this is the c-source code https://github.com/GNOME/gtk/blob/master/gtk/gtkaccelgroup.c)
add a comment |
Accelerator might be a nicer solution but keeping track of keys pressed and released may be closer to what you need.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
self.buffer =
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.button2.connect("key-press-event", self.on_key_down)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyname in self.buffer:
self.buffer.remove(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you released '+'+'.join(self.buffer)+'n')
def on_key_down(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
if keyname in 'wqs' and keyname not in self.buffer:
self.buffer.append(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you pressed '+'+'.join(self.buffer)+'n')
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952407%2fpygtk3-capturing-a-string-from-keybord%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
there is a function(in gtk) which is something similar to your needs..
https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html#gtk-accel-group-connect
it connects a key and a key-mod (https://github.com/bstpierre/gtk-examples/blob/master/c/accel.c)
if you really want to capture a string then you could try recreating the function given in gtk_accel_group
the source can be found in git (this is the c-source code https://github.com/GNOME/gtk/blob/master/gtk/gtkaccelgroup.c)
add a comment |
there is a function(in gtk) which is something similar to your needs..
https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html#gtk-accel-group-connect
it connects a key and a key-mod (https://github.com/bstpierre/gtk-examples/blob/master/c/accel.c)
if you really want to capture a string then you could try recreating the function given in gtk_accel_group
the source can be found in git (this is the c-source code https://github.com/GNOME/gtk/blob/master/gtk/gtkaccelgroup.c)
add a comment |
there is a function(in gtk) which is something similar to your needs..
https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html#gtk-accel-group-connect
it connects a key and a key-mod (https://github.com/bstpierre/gtk-examples/blob/master/c/accel.c)
if you really want to capture a string then you could try recreating the function given in gtk_accel_group
the source can be found in git (this is the c-source code https://github.com/GNOME/gtk/blob/master/gtk/gtkaccelgroup.c)
there is a function(in gtk) which is something similar to your needs..
https://developer.gnome.org/gtk3/stable/gtk3-Keyboard-Accelerators.html#gtk-accel-group-connect
it connects a key and a key-mod (https://github.com/bstpierre/gtk-examples/blob/master/c/accel.c)
if you really want to capture a string then you could try recreating the function given in gtk_accel_group
the source can be found in git (this is the c-source code https://github.com/GNOME/gtk/blob/master/gtk/gtkaccelgroup.c)
edited Dec 28 '18 at 17:57
answered Dec 28 '18 at 17:49
Siva Guru
14114
14114
add a comment |
add a comment |
Accelerator might be a nicer solution but keeping track of keys pressed and released may be closer to what you need.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
self.buffer =
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.button2.connect("key-press-event", self.on_key_down)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyname in self.buffer:
self.buffer.remove(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you released '+'+'.join(self.buffer)+'n')
def on_key_down(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
if keyname in 'wqs' and keyname not in self.buffer:
self.buffer.append(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you pressed '+'+'.join(self.buffer)+'n')
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
add a comment |
Accelerator might be a nicer solution but keeping track of keys pressed and released may be closer to what you need.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
self.buffer =
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.button2.connect("key-press-event", self.on_key_down)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyname in self.buffer:
self.buffer.remove(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you released '+'+'.join(self.buffer)+'n')
def on_key_down(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
if keyname in 'wqs' and keyname not in self.buffer:
self.buffer.append(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you pressed '+'+'.join(self.buffer)+'n')
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
add a comment |
Accelerator might be a nicer solution but keeping track of keys pressed and released may be closer to what you need.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
self.buffer =
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.button2.connect("key-press-event", self.on_key_down)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyname in self.buffer:
self.buffer.remove(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you released '+'+'.join(self.buffer)+'n')
def on_key_down(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
if keyname in 'wqs' and keyname not in self.buffer:
self.buffer.append(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you pressed '+'+'.join(self.buffer)+'n')
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Accelerator might be a nicer solution but keeping track of keys pressed and released may be closer to what you need.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700,550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.create_buttons()
self.buffer =
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 1, 50, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
def create_buttons(self):
self.button1 = Gtk.Button(label="Clear text")
self.button1.connect("clicked", self.on_button1_clicked)
self.grid.attach(self.button1, 0, 0, 1, 1)
self.button2 = Gtk.Button(label="Start capturing")
self.button2.connect("key-release-event", self.on_key_release)
self.button2.connect("key-press-event", self.on_key_down)
self.grid.attach_next_to(self.button2,self.button1,
Gtk.PositionType.RIGHT, 1, 1)
def on_button1_clicked(self, widget):
self.textbuffer.set_text('')
def on_key_release(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if keyname in self.buffer:
self.buffer.remove(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you released '+'+'.join(self.buffer)+'n')
def on_key_down(self, widget, event, data=None):
keyval = event.keyval
keyname = Gdk.keyval_name(keyval)
if keyname in 'wqs' and keyname not in self.buffer:
self.buffer.append(keyname)
if self.buffer:
self.textbuffer.insert_at_cursor('you pressed '+'+'.join(self.buffer)+'n')
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
answered Jan 2 at 16:42
pan-mroku
42828
42828
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53952407%2fpygtk3-capturing-a-string-from-keybord%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown