Solved

Code to set Default Keyboard Layout

Posted on 2000-05-03
6
588 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now