# Trying to Emulate a virtual Keyboard

**URL:** <https://krita-artists.org/t/trying-to-emulate-a-virtual-keyboard/190918>\
**Category:** Developer Questions\
**Created:** [September 23, 2026, 11:40am UTC](https://krita-artists.org/t/trying-to-emulate-a-virtual-keyboard/190918 "2026-09-23T11:40:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [September 23, 2026, 11:40am UTC](https://krita-artists.org/t/trying-to-emulate-a-virtual-keyboard/190918/1 "2026-09-23T11:40:40Z")

</div>

So I am trying to make 2 buttons:

- one of them to press Enter
- another to press Escape.

The objective is to press the button to replace Confirm action and the Cancel action of the shortcuts with an actual button. For example for a crop operation to Confirm it or Cancel it.

I managed to find how to press Enter but I fear it only does that. So when I press the button I trigger:

```auto
def Question_Confirm( self ):
        ki = Krita.instance()
        ad = ki.activeDocument()
        widget = ki.activeWindow().activeView().canvas()
        event = QKeyEvent( QEvent.KeyPress, Qt.Key_Return, Qt.NoModifier )
        QApplication.sendEvent( widget, event )
        self.Wait( ad )

```

However to press escape things do not really work and somehow it is treated as a Confirm action.

```auto
    def Question_Cancel( self ):
        ki = Krita.instance()
        ad = ki.activeDocument()
        widget = ki.activeWindow().activeView().canvas()
        event = QKeyEvent( QEvent.KeyPress, Qt.Key_Escape, Qt.NoModifier )
        QApplication.postEvent( widget, event )
        self.Wait( ad )

```

What am I doing wrong ? or How do you trigger a key shortcut through code ? to activate a action that does not exist ?

---

<div class="post-metadata">

**Author:** ![AkiR](https://krita-artists.org/user_avatar/krita-artists.org/akir/32/8557_2.png) [@AkiR](https://krita-artists.org/u/AkiR)\
**Post date:** [September 23, 2026, 2:15pm UTC](https://krita-artists.org/t/trying-to-emulate-a-virtual-keyboard/190918/2 "2026-09-23T14:15:07Z")

</div>

looks like you are sending / posting events to wrorn objects.

```python
from krita import Krita, Qt, QWidget, QEvent, QApplication, QMdiArea, QKeyEvent, QObject

canvas = Krita.instance().activeWindow().activeView().canvas()
print(f'{canvas = }, {type(canvas) = }, {isinstance(canvas, QWidget) = }')

# you need the actual canvas QWidget, to get it some hacks are needed.

window = Krita.instance().activeWindow()
q_window = window.qwindow()
mdi_area = q_window.findChild(QMdiArea)

target_view = window.activeView()
target_q_view = None
for mdi_window, view in zip(mdi_area.subWindowList(), window.views()):
    if view == target_view:
        target_q_view = mdi_window.widget()
        break

target_q_canvas = None
for w in target_q_view.findChildren(QObject):
    if w.inherits('KisCanvasWidgetBase'):
        target_q_canvas = w
        break
        
print(f'{target_q_view = }, {type(target_q_view) = }, {isinstance(target_q_view, QWidget) = }, {target_q_view.objectName() = }, {target_q_view.metaObject().className() = }')
print(f'{target_q_canvas = }, {type(target_q_canvas) = }, {isinstance(target_q_canvas, QWidget) = }, {target_q_canvas.objectName() = }, {target_q_canvas.metaObject().className() = }')

return_event = QKeyEvent(QEvent.KeyPress, Qt.Key_Return, Qt.NoModifier)
QApplication.sendEvent(target_q_canvas, return_event)

```

/AkiR

---

<div class="post-metadata">

**Author:** ![EyeOdin](https://krita-artists.org/user_avatar/krita-artists.org/eyeodin/32/2662_2.png) [@EyeOdin](https://krita-artists.org/u/EyeOdin)\
**Post date:** [September 23, 2026, 6:35pm UTC](https://krita-artists.org/t/trying-to-emulate-a-virtual-keyboard/190918/3 "2026-09-23T18:35:53Z")

</div>

I think it is working now. look.  
 ![tela_question](https://krita-artists.org/uploads/default/original/3X/9/a/9ab2015ad9add62fea5a186bfae31e616148cfd2.webp)

Thank you @AkiR you are really amazing

---

<div class="post-metadata">

**Author:** ![system](https://krita-artists.org/uploads/default/original/1X/e446d8b3077ddf6dfb5b2cd65199c20095b47dcf.png) [@system](https://krita-artists.org/u/system)\
**Post date:** [September 27, 2026, 6:36pm UTC](https://krita-artists.org/t/trying-to-emulate-a-virtual-keyboard/190918/4 "2026-09-27T18:36:01Z")

</div>

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.
