Solved

Code to set Default Keyboard Layout

Posted on 2000-05-03
6
594 Views
Last Modified: 2013-12-03
Hi,

I need to programmatically set the default keyboard layout.  I've looked around a little at the LoadKeyboardLayout, ActivateKeyboardLayout,  GetKeyboardLayoutList APIs but can't figure out how to use them to accomplish what I want.  Basically, I want to show the user a combobox with possible countries, and based on their selection, set the default keyboard layout.  I would like a small code example if possible.

TIA,
Joe
0
Comment
Question by:joeslow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2784004
I think, you can simple change value in
HKEY_CURRENT_USER\keyboard layout\preload\1 key "default" to
ID of lang. List all Langs see in
description of LoadKeyboardLayout,..
For make list you can use standart ListBox. For change entry in Regitry
I can send you my porogramm(if you have't this one).
Alex
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 200 total points
ID: 2789355
You don't react.

I send you my code for read/write in
system registry

Next is my code for make it
int Test(void)
{
  int bStop = 0;
      // skip registry key opening sequence for clarity
  const INT nSIZE = 4096; //maximum(real 8)
  const DWORD dwLen = 8;
  const char *lpszPathName = "SOFTWARE\\COMPANY\\MYCOMPANY";
  const char *pszValueName = "Param01";

  HKEY hKey,hBaseKey = HKEY_LOCAL_MACHINE;
  DWORD dwType;
  DWORD dwData  = nSIZE;
  char* bData   = (char *)::calloc(nSIZE, sizeof(char));
  struct date dDate;
  LONG retValue;

//open key
  retValue = ::RegOpenKeyEx(hBaseKey, lpszPathName,
       NULL, KEY_ALL_ACCESS,&hKey);
  if (retValue != ERROR_SUCCESS)//no such : 1-st time
    {                           //create key
       uint32 disposition;
       retValue = ::RegCreateKeyEx(hBaseKey, lpszPathName, 0, ""/*class*/,
    REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,
    0/*SecurityAttributes*/, &hKey,
    &disposition);
       if (retValue == ERROR_SUCCESS)//must be!
{
    //you code for form bData
//save data
   ::RegSetValueEx(hKey, pszValueName, 0, REG_BINARY,
  (LPBYTE)bData, dwLen);
   ::RegCloseKey(hKey); //close key
}
    }
  else //exist, read data
    {
      retValue = ::RegQueryValueEx(hKey, pszValueName,
  NULL, &dwType,(LPBYTE)bData, &dwData);

       if (retValue == ERROR_SUCCESS && REG_BINARY == dwType)
{
   if (Empty(bData)) //no data?
     {
       //you code for form bData
// Save
      ::RegSetValueEx(hKey, pszValueName, 0, REG_BINARY,
  (LPBYTE)bData, dwLen);
     }
   else  //test data
     {
     }

}
      ::RegCloseKey(hKey); //close key
    }
  if (bData)
    free (bData);
  return bStop;
}
Alex
0
 

Author Comment

by:joeslow
ID: 2789512
Hi Alex,

I saw your reply this morning and have been playing around with it a little.  I am not sure it does all the necessary things though.  For example:

I go to the control panel and start the keyboard cpl, then set the default locale input to something different, hit apply, start notepad, and  the new language takes effect.  

If I go directly to regedit, change it myself, then start notepad, the change does not seem to take effect.

There seems to be a place under HKEY_USERS\.DEFAULT that may do the trick as you described but I haven'tr tried yet...

Thanks,
Joe
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Expert Comment

by:meamox
ID: 2823799
The value under HKEY_USERS\.DEFAULT\Keyboard Layout\Preload will only change the keyboard layout for the default, non-logged on user. (i.e In Windows NT, the keyboard layout you are typing with when you are typing your account and password to logon in the security dialog.)  Once you log on, the settings in HKEY_CURRENT_USER take over.


   I've been attempting to do something similar to change the keyboard layout for all threads for a given user, but for some reason, the LoadKeyboardLayout API call (with KLF_ACTIVATE flag) only changes the layout for the current thread (the one that changes the layout) in Windows NT.  That is the documented behavior for Windows 9x, but for Windows NT it is supposed to change the layout for more than just the current thread.

I can post my code if you are interested, but as I'm sort of still looking for the same answers that you are, I'll assume for now that it wouldn't help much.  =)
0
 

Author Comment

by:joeslow
ID: 2825753
That explains alot.  I was thinking the default user was used whenever a new user was created.  So are you saying you can not set the default keyboard layout for the "computer" to Swedish, so that all users created will have this as a default?

Thanks,
Joe
0
 

Author Comment

by:joeslow
ID: 2834882
Alex,

Sorry it took so long to get back on this.  I had to settle on setting the HKEY_CURRENT_USER\Keyboard\Preload\1 key as you suggested in your first reply.  I was hoping I could set it for the whole system, not just one user...

Thanks again,
Joe
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question