?
Solved

Change keyboard layout

Posted on 2004-09-21
21
Medium Priority
?
996 Views
Last Modified: 2012-05-05
Hi,
  I'm looking for a vbs script that can change the keyboard layout on a given computer ( running Windows 2000 / XP ). The problem we're experiencing is for some reason when some users log on, they get the US keymap instead of the UK keymap. This means that they can't get several crucial characters and end up complaining. So, does anyone have a vb script that I could get them to run on login that would automatically set the right keyboard? The users do have permission to change keyboard but not the know how.

Hope you guys can help!
0
Comment
Question by:petepalmer
  • 5
  • 5
  • 5
  • +3
19 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12113735
Have you tried
    SetLocale 2057    '2057 being the UK code

or
    SetLocale en-gb
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12113782
tried that - doesn't seem to do anything I'm afraid
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12114028
You can use WinAPI functio LoadKeyboardLayout:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput
/keyboardinputreference/keyboardinputfunctions/loadkeyboardlayout.asp

Also take a look to this article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;139571
To implement in Visual basic, take a look to:
http://www.mentalis.org/apilist/LoadKeyboardLayout.shtml

Good luck,
Jaime.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12114111
Notice that changing Windows Registry directly, you will not have an inmediat effect. But still will be useful to "scan" registry to know installed keyboard layouts, as pointed out in MSDN article I've posted.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12114166
Out of interest, Jaime, how do you declare that function from within a VBS?

I tried

   Declare Function LoadKeyboardLayout Lib "user32.dll" Alias "LoadKeyboardLayoutA" (ByVal pwszKLID As String, ByVal flags As Long) As Long

as I would in VB6 and it failed with a syntax error...
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12114443
try to remove the alias (Alias "LoadKeyboardLayoutA")
0
 
LVL 14

Expert Comment

by:Matti
ID: 12114686
Hi!

This is module level code:

Public Userkeyboard As String
Public Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutA" (ByVal pwszKLID As String, ByVal Flags As Long) As Long
Public Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long
Public Const KLF_ACTIVATE = &H1


Public Sub Askkeyboard()
On Error Resume Next
Dim X&, pwszKLI2 As String * 8
X = GetKeyboardLayoutName(pwszKLI2)
Userkeyboard = pwszKLI2
End Sub

Public Sub Restorekeyboard()
Call LoadKeyboardLayout(Userkeyboard, KLF_ACTIVATE)
End Sub

Public Function ChKeyboard(langnum As Long) As Long
On Error Resume Next
Dim X&, setKB As String
DoEvents
Select Case asettelu
Case 0
setKB = "00000419"
Case 1
setKB = "00010419"
Case 2
setKB = "00000423"
Case Else
setKB = "00000419"
End Select
DoEvents
Call LoadKeyboardLayout(setKB, KLF_ACTIVATE)
End Function

Test code on form:
Private Sub Form_Load()
Askkeyboard
Call ChKeyboard(2)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Restorekeyboard
End Sub

More layouts and their constant values you will found from registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Nls\LocaleMapIDs
Install more keyboards from windows CD.


Matti

0
 
LVL 1

Author Comment

by:petepalmer
ID: 12114985
Well I'm looking for some code that I can run as is to set the keyboard to gb :)
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12121344
Jaime/Matti

Declare Function LoadKeyboardLayout Lib "user32.dll" (ByVal pwszKLID As String, ByVal flags As Long)

returns a syntax error at char 9, when used in a VBS; which looks like it doesn't like the word 'Function'.

How do you call from the API, in a VBS?
0
 
LVL 14

Expert Comment

by:Matti
ID: 12122041
Hi, jimbobmcgee!

It's important that this pwszKLID string is fixed lenght 8 char long, won't work if it's not. I don't use VBS on this macine so can't test, but think there is a bug on this API function.

Matti
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12122715
I think there is no problem with pwszKLID, but, since you are declaring a function you must declare return type:
Declare Function LoadKeyboardLayout Lib "user32.dll" (ByVal pwszKLID As String, ByVal flags As Long) as Long
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12123459
This is all very interesting but I'm not sure how it solves my problem :)

For the 500 points I either need a working VB script or a compiled VB application - as I don't have Visual Studio to compile it myself.

Thanks in advance!
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12128164
> I think there is no problem with pwszKLID, but, since you are declaring a function you must declare return type:
> Declare Function LoadKeyboardLayout Lib "user32.dll" (ByVal pwszKLID As String, ByVal flags As Long) as Long

Still it returns a syntax error at char 9 (starting at function)

> This is all very interesting but I'm not sure how it solves my problem :)

If you cannot 'declare' the LoadKeyboardLayout API, you can't use the above code in VBS.  If you want an EXE, send an email to whoever you prefer; I'm sure the listed code will work...

:)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12128349
Take a look to this article about using WinAPI functions in VBScript. Hope to be useful.
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_10115990.html
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12563839
I was unable to use any of the options to do what I wanted therefore I'm going to request a refund.
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12563887
Looking back through the posts, whilst the information might be perfectly acceptable, I was unable to use it to create a script or program that would allow users to set their own keyboard. If someone can provide the information in a script or program form that works, I'll happily accept it - but as it stands I'm not able to use the information I'm afraid.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12697501
As mentioned, if you want an executable, send someone here an email (like me) and we'll compile for you, using one of the solutions you choose above...

J.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12848596
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

599 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