5

Is it possible to change the Input Keyboard Layouts by programmatically in Pyqt5?

My first and second text box accepts Tamil letters. IN Tamil So many keyboard Layouts available. By default in Windows 10, Tamil Phonetic, Tamil99 and Tamil Traditional Keyboards are available. Now I want to select Keybaord layouts programmatically...

For example. In My First textbox, I need to assign a "Tamil99" keyboard layout and in the second textbox, I need to assign a "Tamil Phonetic" keyboard layout. How to assign it, programmatically?

import sys

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Diff_Language(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("InPut Different languges in Different Textbox")
        self.lbl1 = QLabel("Input Language - Tamil99 Keyboard")
        self.lbl2 = QLabel("Input Language - Tamil phonetic keyboard")


        self.tbox1 = QLineEdit()
        self.tbox1.setFont(QFont('senthamil', 10, QFont.Bold))

        self.tbox2 = QLineEdit()
        self.tbox2.setFont(QFont('senthamil', 30, QFont.Bold))

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

        self.setLayout(self.vbox)

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

if __name__ == '__main__':
    main()
tckraomuqnt
  • 470
  • 4
  • 17

3 Answers3

5

So Qt doesn't offer this, but you can ask your OS to do it for you.

Assuming you're just looking at Windows, you can change the current keyboard layout in python using pywin32, which lets you easily access the Windows API from your script. Once installed into your Python environment you can import win32api and then use a call like win32api.LoadKeyboardLayout('00000809',1) to set the layout (the values I've put here set it to UK English). The first parameter is a string representing the keyboard layout to use, and the second is a flag. See documentation.

I found this list of KLIDs (Keyboard Layout IDs), which shows two for Tamil keyboards. "00000449" is Tamil, "00020449" is Tamil 99. The 449 at the end means Tamil, and the two digits before set which subtype of Tamil keyboard to use (e.g. 20 for Tamil 99) - I can't find one for Tamil phonetic, but maybe you'll be able to find it.

You can set up your program to call these functions whenever you want it to switch keyboard (for example, when your user activates a specific text input box).

Also, if you want to check the current keyboard layout you can use win32api.GetKeyboardLayout(0) (doc). Maybe you can use this to figure out what the IDs are for each of the Tamil keyboards you want to use. Mind that it returns an int for the locale id rather than a string.

Other useful keyboard related functions are win32api.GetKeyboardLayoutList() (to find all the locales installed on the current machine), win32api.GetKeyboardLayoutName() and win32api.GetKeyboardState - documentation for all these can be found here.

JRVeale
  • 394
  • 2
  • 10
  • 2
    (I can't read or speak Tamil, so I apologise if I've got the keyboards mixed up!) – JRVeale Mar 08 '22 at 17:14
  • 1
    Thanks. Load/change/get Keyboard layout details. But I Struggling with, How to Implement it in my GUI. For Example, Change/Load desired Keybaord layout, But when I try typing after this, in my textbox, it continues to type normal English characters. only – tckraomuqnt Mar 08 '22 at 23:58
  • 2
    @tckraomuqnt I don't know how keyboard layout works nowadays, but if your alternate layout is not the default one, it's possible that the system automatically resets to the default one when switching application on focus changes. Try to implement the above within the `focusInEvent()` override of the input widget, *after* calling the base implementation. – musicamante Mar 09 '22 at 01:28
3

Your code works perfectly fine for me. And there is no need to change your keyboard layout for senthamil font. Make sure that you have senthamil font installed on your computer. Link to download senthamil

If you want to change keyboard layouts programmatically you can do something like this:

from win32api import LoadKeyboardLayout # pypi.org/project/pywin32/


class Diff_Language(QWidget):

    def box2Pressed(self, event):
        print("Pressed 2nd box")
        LoadKeyboardLayout("00000409", 1) # https://stackoverflow.com/a/15770148/16479143
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("InPut Different languges in Different Textbox")
        self.lbl1 = QLabel("Input Language - Tamil99 Keyboard")
        self.lbl2 = QLabel("Input Language - Tamil phonetic keyboard")


        self.tbox1 = QLineEdit()
        self.tbox1.setFont(QFont('senthamil', 10, QFont.Bold))

        self.tbox2 = QLineEdit()
        self.tbox2.mousePressEvent = self.box2Pressed # function is called everytime this widget is pressed.
        self.tbox2.setFont(QFont('senthamil', 30, QFont.Bold))

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

        self.setLayout(self.vbox)

Typing jkpo; in the second box will type Tamil in Tamil.

sabz
  • 460
  • 1
  • 4
  • 10
2

It has been already replied for windows os, if you need it for linux system, you have to use setxbmap command. or this kind of python api/utility https://pypi.org/project/keyboard-switch/

sancelot
  • 1,905
  • 12
  • 31