Skip to main content

A simple and customizable hotkey picker widget for PyQt and PySide

Project description

PyQt Hotkey Picker

PyPI Python Build Coverage License

A simple and customizable hotkey picker widget for PyQt and PySide

pyqthotkey

Features

  • Simple and fully customizable UI
  • Supports 469 different keys
  • Supports whitelisting and blacklisting keys
  • Supports customizing key names
  • Works with PyQt5, PyQt6, PySide2, and PySide6

Installation

pip install pyqthotkey

Usage

Import the HotkeyPicker class and add it to your window like any other PyQt widget:

from PyQt6.QtWidgets import QMainWindow
from PyQt6.QtCore import Qt
from pyqthotkey import HotkeyPicker


class Window(QMainWindow):
    def __init__(self):
        super().__init__(parent=None)

        hotkey_picker = HotkeyPicker(self)  # Add hotkey picker with default settings
        hotkey_picker.hotkeyChanged.connect(self.hotkey_changed)  # Connect change event
    
    # Called every time the picked hotkey changes
    def hotkey_changed(self, key, key_name):
        print([key, key_name])

Use the getHotkey() and getHotkeyName() methods to get the key code and name of the selected hotkey:

# Returns int with key code if a hotkey is selected, otherwise None
key_code = hotkey_picker.getHotkey()  # e.g. 65 (which is the same as Qt.Key.Key_A)

# Returns string with key name if a hotkey is selected, otherwise None
key_name = hotkey_picker.getHotkeyName()  # e.g. 'Shift'

Manually set the selected hotkey with the setHotkey() method:

hotkey_picker.setHotkey(Qt.Key.Key_A)  # Could also directly pass int (e.g. 65)

Reset the hotkey picker to the default state with no selected hotkey by using the reset() method:

hotkey_picker.reset()

You can also use the static getKeyName() method to get the name of a key:

key_name_a = HotkeyPicker.getKeyName(Qt.Key.Key_A)  # 'A'
key_name_f5 = HotkeyPicker.getKeyName(16777268)     # 'F5'

More in-depth examples can be found in the examples folder.

Customization

  • Overriding key names (static):

    # Change control key name from default 'Control' to 'Ctrl'
    HotkeyPicker.setKeyName(Qt.Key.Key_Control, 'Ctrl')
    
  • Changing the cancel key used to exit the hotkey selection:

    # On initialization
    hotkey_picker = HotkeyPicker(self, cancel_key=Qt.Key.Key_Return)
    
    # Or using the setter
    hotkey_picker.setCancelKey(Qt.Key.Key_Return)  # Default: Qt.Key.Key_Escape
    
  • Changing the default text of the hotkey picker:

    # On initialization
    hotkey_picker = HotkeyPicker(self, default_text='Not selected..')
    
    # Or using the setter
    hotkey_picker.setDefaultText('Not selected..')  # Default: 'None'
    
  • Changing the text of the hotkey picker that is shown when waiting for a key press:

    # On initialization
    hotkey_picker = HotkeyPicker(self, selection_text='Selecting..')
    
    # Or using the setter
    hotkey_picker.setSelectionText('Selecting..')  # Default: '..'
    
  • Only allowing specific keys to be selected:

    # List of whitelisted keys (no other key can be selected)
    keys = [Qt.Key.Key_F1, Qt.Key.Key_F2, Qt.Key.Key_F3, Qt.Key.Key_F4,
            Qt.Key.Key_F5, Qt.Key.Key_F6, Qt.Key.Key_F7, Qt.Key.Key_F8,
            Qt.Key.Key_F9, Qt.Key.Key_F10, Qt.Key.Key_F11, Qt.Key.Key_F12]
    
    # On initialization 
    hotkey_picker = HotkeyPicker(self, key_filter_enabled=True, whitelisted_keys=keys)
    
    # Or using the setter
    hotkey_picker.setKeyFilterEnabled(True)  # Default: False
    hotkey_picker.setWhitelistedKeys(keys)   # Default: []
    
  • Not allowing specific keys to be selected:

    # List of blacklisted keys (every other key can be selected)
    keys = [Qt.Key.Key_F1, Qt.Key.Key_F2, Qt.Key.Key_F3,
            Qt.Key.Key_F4, Qt.Key.Key_F5]
    
    # On initialization 
    hotkey_picker = HotkeyPicker(self, key_filter_enabled=True, blacklisted_keys=keys)
    
    # Or using the setter
    hotkey_picker.setKeyFilterEnabled(True)  # Default: False
    hotkey_picker.setBlacklistedKeys(keys)   # Default: []
    

    Note:
    Only one filter option can be enabled per hotkey picker at any given time, meaning you can either specify the whitelisted keys, specify the blacklisted keys, or not enable key filtering at all.

Tests

Installing the required test dependencies PyQt6, pytest, and coveragepy:

pip install PyQt6 pytest coverage

To run the tests with coverage, clone this repository, go into the main directory and run:

coverage run -m pytest
coverage report --ignore-errors -m

License

This software is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqthotkey-3.0.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

pyqthotkey-3.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pyqthotkey-3.0.0.tar.gz.

File metadata

  • Download URL: pyqthotkey-3.0.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.3

File hashes

Hashes for pyqthotkey-3.0.0.tar.gz
Algorithm Hash digest
SHA256 0253c4602357f4eaec5328bec8c29fe5a6c8dc2a994a67f5806f9001a6ee2913
MD5 b3baed2a6efb3506bfb578573ca37512
BLAKE2b-256 f9459d02bc644559333f47a73c83762ae785fa6cb80000da83d8704e08976c03

See more details on using hashes here.

File details

Details for the file pyqthotkey-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyqthotkey-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.3

File hashes

Hashes for pyqthotkey-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e86041e4daaa4eae006c78402c32d608042d4d2c362ff648b6e53662c0a2c2d
MD5 8b2f7fddaaa456729779febbc4588939
BLAKE2b-256 efcb6d548ca0bc22ca3b19cb20393fbaee779313dfb5fcf328aa5fa1001a01ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page