from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class Foo(QLineEdit):
    def keyPressEvent(self, event):
        print 'Foo keyPress', event.key(); sys.stdout.flush()
        QLineEdit.keyPressEvent(self, event)

app = QApplication([])
window = QMainWindow()
f = Foo()
window.setCentralWidget(f)
window.show()

def do_action():
    print 'action'; sys.stdout.flush()
action1 = QAction('Action 1', f, triggered=do_action)
action1.setShortcut(Qt.CTRL + Qt.Key_Z)
action1.setShortcutContext(Qt.WidgetShortcut)

action2 = QAction('Action 2', f, triggered=do_action)
action2.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_R))

menu = window.menuBar().addMenu('Menu')
menu.addAction(action1)
menu.addAction(action2)

app.exec_()

