Python + PySimpleGUI: INFINITY KEY (8.6.22)

Posted by


In this tutorial, we will explore how to use the INFINITY KEY 8.6.22 Python library in combination with PySimpleGUI to create a simple graphical user interface (GUI). INFINITY KEY is a powerful Python library that allows you to create custom keyboard layouts for use in various applications, while PySimpleGUI is a lightweight and easy-to-use GUI library for Python.

Step 1: Installation

Before we can get started, we need to install both the INFINITY KEY library and PySimpleGUI. You can install these libraries using pip, the Python package installer. Open a terminal window and run the following commands:

pip install infinitykey
pip install PySimpleGUI

Step 2: Importing the necessary libraries

Next, we need to import the necessary libraries in our Python script. Create a new Python script and add the following lines at the beginning:

import PySimpleGUI as sg
from infinitykey import InfinityKey

Step 3: Creating the INFINITY KEY instance

Now, let’s create an instance of the InfinityKey class. We will also define the keymap that we want to use. In this example, we will create a simple keymap with letters A to Z:

keymap = {
    (12, 5): 'A', (13, 5): 'B', (14, 5): 'C', (15, 5): 'D', (16, 5): 'E',
    (12, 4): 'F', (13, 4): 'G', (14, 4): 'H', (15, 4): 'I', (16, 4): 'J',
    (12, 3): 'K', (13, 3): 'L', (14, 3): 'M', (15, 3): 'N', (16, 3): 'O',
    (12, 2): 'P', (13, 2): 'Q', (14, 2): 'R', (15, 2): 'S', (16, 2): 'T',
    (12, 1): 'U', (13, 1): 'V', (14, 1): 'W', (15, 1): 'X', (16, 1): 'Y',
    (13, 0): 'Z'
}

ik = InfinityKey(keymap)

Step 4: Creating the PySimpleGUI window

Now, let’s create the PySimpleGUI window with a text input field where the user can type. We will also add a layout for the INFINITY KEY keyboard:

layout = [
    [sg.Input(size=(20,1), key='input')],
    [sg.Graph((20, 5), (20, 5), (0, 5), key='graph')]
]

window = sg.Window('INFINITY KEY', layout)

Step 5: Main loop

Finally, we will enter the main event loop where we listen for key presses on the INFINITY KEY keyboard and update the text input field accordingly:

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

    key = ik.get_key()

    if key is not None:
        window['input'].update(values['input'] + key)

Step 6: Running the script

Save your script and run it. You should see a PySimpleGUI window with a text input field and the INFINITY KEY keyboard layout. You can now type using the INFINITY KEY keyboard and see the text being updated in the input field.

Congratulations! You have successfully created a simple GUI using INFINITY KEY and PySimpleGUI. Feel free to customize the layout and functionality to suit your needs. You can also explore the INFINITY KEY documentation for more advanced features and options.