real-time text input issues in PyQt5 with Tamil99 keyboard layout

71 views Asked by At

I am working on a PyQt5 application with three QLineEdit widgets and one QListWidget. The first QLineEdit is for English (US) input, the second uses the Tamil Phonetic input method, and the third uses the Tamil99 keyboard layout. The application runs on Windows 10.

The issue arises when using the Tamil99 keyboard layout in the third QLineEdit. The textChanged signal is not emitted for every key press, and as a result, the filtered list in the QListWidget is not updated in real-time. How to resolve it ?

Problem: When using a QLineEdit with the Tamil99 keyboard layout, the textChanged signal is not emitted for every key press, and the filtered list is not updated in real-time.

Requirements: Real-time Filtering: The application should capture every keystroke in the QLineEdit widgets and update the filtered list in the QListWidget in real-time.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import win32api
import py_win_keyboard_layout

class Diff_Language(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Input Different Languages in Different Textboxes")

        self.lbl1 = QLabel("Input Language - English (US)")
        self.tbox1 = QLineEdit()
        self.tbox1.setFont(QFont('Arial Unicode MS', 10, QFont.Bold))
        self.tbox1.installEventFilter(self)
        self.tbox1.setMaxLength(25)
        self.tbox1.textChanged.connect(self.filter_list)

        self.lbl2 = QLabel("Input Language - Tamil Phonetic")
        self.tbox2 = QLineEdit()
        self.tbox2.setFont(QFont('Arial Unicode MS', 30, QFont.Bold))
        self.tbox2.installEventFilter(self)
        self.tbox2.setMaxLength(25)
        self.tbox2.textChanged.connect(self.filter_list)

        self.lbl3 = QLabel("Input Language - Default (English US)")
        self.tbox3 = QLineEdit()
        self.tbox3.setFont(QFont('Arial Unicode MS', 12, QFont.Bold))
        self.tbox3.installEventFilter(self)
        self.tbox3.setMaxLength(25)
        self.tbox3.textChanged.connect(self.filter_list)

        self.listbox = QListWidget()
        add_items = ["பாலாஜி","பாலா","பால்","Red", "Dark red", "Light Red", "Redish Blue", "Redish Green", "Green","Blue","Dark Blue", "Dark Green"]
        self.original_items = add_items.copy()  # Store original items for filtering
        self.listbox.addItems(add_items)

        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.lbl1)
        self.vbox.addWidget(self.tbox1)
        self.vbox.addWidget(self.lbl2)
        self.vbox.addWidget(self.tbox2)
        self.vbox.addWidget(self.lbl3)
        self.vbox.addWidget(self.tbox3)
        self.vbox.addWidget(self.listbox)

        self.setLayout(self.vbox)
        self.current_layout = 0x409  # Default layout is English (US)

    def eventFilter(self, obj, event):
        if event.type() == QEvent.FocusIn:
            self.handle_focus_in(obj)
        return super().eventFilter(obj, event)


    def handle_focus_in(self, textbox):
        if textbox == self.tbox1:
            self.current_layout = win32api.LoadKeyboardLayout("00020409")

        elif textbox == self.tbox2:
            self.current_layout = 0x449  # Tamil Phonetic language identifier

        elif textbox == self.tbox3:
            self.current_layout = win32api.LoadKeyboardLayout("00020449") #Tamil99 keyboard layout

        py_win_keyboard_layout.change_foreground_window_keyboard_layout(self.current_layout)

        self.listbox.clear()
        self.listbox.addItems(self.original_items)

    def key_press_event(self, event):
        super(QLineEdit, self.tbox1).keyPressEvent(event)
        text = self.tbox1.text()
        print("1111111", text)
        filtered_items = [item for item in self.original_items if text.lower() in item.lower()]
        self.listbox.clear()
        self.listbox.addItems(filtered_items)
        
    def filter_list(self):
        text = self.sender().text()  # Get the text from the sender
        print("1111111",  text)

        filtered_items = [item for item in self.original_items if text in item.lower()]
        self.listbox.clear()
        self.listbox.addItems(filtered_items)

    def handle_focus_out(self, textbox):
        # When a textbox loses focus, switch back to the default layout (English US)
        self.current_layout = win32api.LoadKeyboardLayout("00020409")
        py_win_keyboard_layout.change_foreground_window_keyboard_layout(self.current_layout)

    def closeEvent(self, event):
        self.current_layout = win32api.LoadKeyboardLayout("00020409")
        py_win_keyboard_layout.change_foreground_window_keyboard_layout(self.current_layout)
        event.accept()

def main():
    app = QApplication(sys.argv)
    mainscreen = Diff_Language()
    app.setStyle("Fusion")
    mainscreen.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

For example if we type 'j' and 'Q' simultaneously its equivalent to "பா" and type "n" and "q" simultaneously its equivalent to "லா"

0

There are 0 answers