Solved

Code to set Default Keyboard Layout

Posted on 2000-05-03
6
590 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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