File: keymapping.py

package info (click to toggle)
pymol 2.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,312 kB
  • sloc: cpp: 480,106; python: 79,860; ansic: 28,343; javascript: 6,792; sh: 47; makefile: 30; csh: 8
file content (99 lines) | stat: -rw-r--r-- 2,012 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'''
Module for translating Qt key codes to PyMOL key and "special" codes
'''

from __future__ import absolute_import

from pymol.Qt import QtCore
Qt = QtCore.Qt

DEBUG = False

keyMap = {
    Qt.Key_Escape: 27,
    Qt.Key_Tab: 9,
    Qt.Key_Backspace: 8,
    Qt.Key_Return: 13,
    Qt.Key_Enter: 13,
    Qt.Key_Delete: 127,
}

specialMap = {
    Qt.Key_Left: 100,
    Qt.Key_Up: 101,
    Qt.Key_Right: 102,
    Qt.Key_Down: 103,
    Qt.Key_PageUp: 104,
    Qt.Key_PageDown: 105,
    Qt.Key_Home: 106,
    Qt.Key_End: 107,
    Qt.Key_Insert: 108,
    Qt.Key_F1: 1,
    Qt.Key_F2: 2,
    Qt.Key_F3: 3,
    Qt.Key_F4: 4,
    Qt.Key_F5: 5,
    Qt.Key_F6: 6,
    Qt.Key_F7: 7,
    Qt.Key_F8: 8,
    Qt.Key_F9: 9,
    Qt.Key_F10: 10,
    Qt.Key_F11: 11,
    Qt.Key_F12: 12,
}


def get_modifiers(ev):
    '''Get modifers from event and translate into PyMOL modifier mask'''
    pymolmod = 0
    qtmodifiers = ev.modifiers()

    for mask, qtm in [
        (0x1, Qt.ShiftModifier),
        (0x2, Qt.MetaModifier),  # CTRL on Mac
        (0x2, Qt.ControlModifier),
        (0x4, Qt.AltModifier)
    ]:
        if qtmodifiers & qtm:
            pymolmod |= mask

    return pymolmod


def keyPressEventToPyMOLButtonArgs(ev):
    # translate modifier mask
    pymolmod = get_modifiers(ev)

    # Qt::Key_*
    key = ev.key()

    if key in specialMap:
        k = specialMap[key]

        # PyMOL_Special
        state = -2
    else:
        # PyMOL_Key
        state = -1

        # doesn't work on Mac: k = keyMap.get(key, ev.nativeVirtualKey())
        k = keyMap.get(key, -1)
        if k == -1:
            text = ev.text()
            if text:
                k = ord(text)

        # CTRL-<key>
        if k == -1 and (pymolmod & 0x2):
            k = key - 64

        # ALT-<key>
        if k != -1 and (pymolmod & 0x4):
            k = key

        if k > 255 or k < 0:
            if DEBUG:
                print('DEBUG: skipped: 0x%x 0x%x' % (key, k))
            return

    return (k, state, 0, 0, pymolmod)