Someone
Someone

Reputation:

Change keyboard layout with python?

I'm working on a college system (windows XP) and want to set the keyboard to Dvorak when I log on. I currently have a python script that changes the desktop image.

Can I use python to change the layout as well? Or are there other ways?

Upvotes: 5

Views: 11429

Answers (4)

Oleksii Sylichenko
Oleksii Sylichenko

Reputation: 51

For those, who found this post after 2024 and code from previous answers does not work - you just need to get foreground window and send language changing message to it:

import win32gui
import win32api
import win32process

WM_INPUTLANGCHANGEREQUEST = 0x0050  # win32api const
EN = 0x4090409  # English
UK = -0xf57fbde # Ukrainian

window_handle = win32gui.GetForegroundWindow()
win32api.PostMessage(window_handle, WM_INPUTLANGCHANGEREQUEST, 0, EN)
# win32api.PostMessage(window_handle, WM_INPUTLANGCHANGEREQUEST, 0, UK)

Upvotes: 1

user2229472
user2229472

Reputation: 509

to Change keyboard layout

import win32api
win32api.LoadKeyboardLayout('00000409',1) # to switch to english
win32api.LoadKeyboardLayout('00000401',1) # to switch to arabic

and for Dvorak :

win32api.LoadKeyboardLayout("00010409",1)

or

win32api.LoadKeyboardLayout("00020409",1)

Upvotes: 3

user68370
user68370

Reputation:

I would use AutoHotKey to change the layout. You could write a script remapping the keys and compile it as an executable file.

For example

q::'
+q::"
w::,
+w::<
e::.
+e::>
r::p

etc.

Upvotes: 1

Tzury Bar Yochay
Tzury Bar Yochay

Reputation: 9004

answer can be found at Programmatically change keyboard to Dvorak

Upvotes: 1

Related Questions