Programmatically change Windows keyboard language

6 views (last 30 days)
Dear colleagues
I am writing an application which uses the numbers in the top row of the keyboard as keyboard shortcuts to trigger some functions. I use WindowKeyPressFcn and event.Key to get which key was pressed by the user.
I live in Czechia and most people here have two keyboards installed on their computers: Czech keyboard and English keyboard. On the Czech keyboard, however, the numbers in the top row are substituted by letters with accents (in Czech language we use them a lot). My application, however, does not work when the input is those special Czech characters instead of numbers.
1) Is there a way to read the key codes instead of the current character, which would be independent of which keyboard is set in Windows?
2) Is there a way to programmatically change the keyboard to English (e.g. whenever the user runs my application)?
I am using Matlab R2019b.
Thank you for any suggestions
Jan Kudláček (you can see the letters with accents in my surname :-) )

Answers (1)

Rahul
Rahul on 7 May 2025
I understand that you face the issue of 'WindowKeyPressFcn' not recognising the corrcet key press for Czech characters. While there is no Native solution for MATLAB for this case. Here is a workaround which might be useful:
  • Mapping the required key characters from the Czech keyboard to English. Here is an example:
function myKeyPressFcn(src, event)
czechToEnglish = struct('plus','1','ě','2','š','3','č','4','ř','5','ž','6','ý','7','á','8','í','9','é','0');
if isfield(czechToEnglish, event.Key)
mappedKey = czechToEnglish.(event.Key);
disp(['Mapped to: ', mappedKey]);
% call your function for the number here
else
disp(['Pressed: ', event.Key]);
end
end
MATLAB itself cannot change the Keyboard Layout to English. However, if required you can make use of system commands or third party libraries.
Thanks.
  3 Comments
Steven Lord
Steven Lord on 7 May 2025
You could use a dictionary instead.
czechToEnglish = dictionary('plus','1','ě','2','š','3','č','4','ř','5',...
'ž','6','ý','7','á','8','í','9','é','0');
czechToEnglish('ě')
ans = "2"
Or if you want the values to be numbers:
czechToEnglish2 = dictionary('plus',1,'ě',2,'š',3,'č',4,'ř',5,...
'ž',6,'ý',7,'á',8,'í',9,'é',0);
czechToEnglish2('ě')
ans = 2

Sign in to comment.

Categories

Find more on Desktop in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!