Change keyboard layout with python?
Asked Answered
L

4

5

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?

Leacock answered 14/9, 2009 at 11:11 Comment(4)
One answer in #167531 that doesn't programmatically change to the Dvorak layout but does remap all the keys -- that might be fine since registry settings will be cleared when you log out, assumably.Creighton
But how can you use vim with Dvorak? :/Pelmas
I actually used vim with Dvorak for a while (back in aught-one). Bit of a mind blower. I got used to it, but I don't necessarily recommend it.Befit
I didn't learn vim until well after converting to Dvorak, so vim with Dvorak works fine for me.Spirochaetosis
A
3

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)
Astigmatic answered 2/4, 2013 at 17:12 Comment(6)
Do you have the keyboard layout code for Dvorak or a link to the documentation for win32api?Antiscorbutic
use this to get your keyboard layout code: win32api.GetKeyboardLayout()Astigmatic
Thanks; but I don't want to find my own, I want to know whether loadKeyboardLayout has a Dvorak option, which is what OP is looking for.Antiscorbutic
I think the keyboard layout code for Dvorak is ("00010409" or "00020409")Astigmatic
I don't know - I asked because that was what the OP was asking for. Thanks for adding it to the answer!Antiscorbutic
This doesn't really change current layout for me. Instead, it just adds new one to current list.Police
E
1

answer can be found at Programmatically change keyboard to Dvorak

Epigastrium answered 14/9, 2009 at 11:19 Comment(1)
comments are not supposed to be posted as answers.Lawrenson
W
1

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.

Wilsonwilt answered 9/2, 2011 at 15:6 Comment(0)
B
0

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)
Baker answered 4/4, 2024 at 19:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.