Click here to Skip to main content
15,924,317 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a keyboard layout KEY file for my language. It's not a windows default keyboard layout file. Now I want to set it globally through a VB application. It will have a form and two button one to set the custom keyboard layout and other button will revert it form windows default. Can anybody help? Thanks in advance.

They keyboard file is here

Zippyshare.com - hindi_1.key[^]

What I have tried:

Public Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutA" (ByVal pwszKLID As String, ByVal Flags As Long) As Long

LoadKeyboardLayout("00000429", 1)
Posted
Updated 20-May-16 14:39pm

Thank you Sergey for your help. By following your suggestions I come to the following code:
VB
Option Strict Off
Option Explicit On
Friend Class Form1
	Inherits System.Windows.Forms.Form
	Private Const KL_NAMELENGTH As Integer = 9
	Private Const HKL_NEXT As Integer = 1
	Private Const HKL_PREV As Integer = 0
	
	Private Declare Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As Integer, ByVal Flags As Integer) As Integer
	
	Private Declare Function GetKeyboardLayout Lib "user32" (ByVal dwLayout As Integer) As Integer
	
	Private Declare Function GetKeyboardLayoutName Lib "user32"  Alias "GetKeyboardLayoutNameA"(ByVal pwszKLID As String) As Integer
	
	Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
		Dim lngCurrent_HKL As Integer
		Dim strCurrent_Buffer As String
		Dim lngSwitched_HKL As Integer
		Dim strSwitched_Buffer As String
		
		'Retrieves current handle to the keyboard layout
		lngCurrent_HKL = GetKeyboardLayout(0)
		
		'creates a buffers
		strCurrent_Buffer = New String(Chr(0), KL_NAMELENGTH - 1)
		strSwitched_Buffer = New String(Chr(0), KL_NAMELENGTH - 1)
		
		'Retrieves current name of the active keyboard layout
		GetKeyboardLayoutName(strCurrent_Buffer)
		
		'Switches to the next keyboard layout
		ActivateKeyboardLayout(HKL_NEXT, 0)
		
		'Retrieves "switched" handle to the keyboard layout
		lngSwitched_HKL = GetKeyboardLayout(0)
		
		'Retrieves "switched" name of the active keyboard layout
		GetKeyboardLayoutName(strSwitched_Buffer)
		MsgBox("Keyboard layout was switched" & vbCrLf & "from " & strCurrent_Buffer & " to " & strSwitched_Buffer)
		
		'Returns to the initial keyboard layout
		'ActivateKeyboardLayout lngCurrentHKL, 0
	End Sub
	
	
	
End Class
 
Share this answer
 
File? No, this is not how keyboard layout works. Keyboard layout is a special executable module which can be installed in the system. When it is installed, it can be added the sequence of keyboard layouts (actually, input languages) accessible to a particular user. And then the user can choose which one to use for each application at each moment of time.

If you want to know how to create your own keyboard layout and install it, I can help you. There are many cases when it is very useful or even necessary.

But you want to do a different thing: enforce some keyboard layout "globally". This is not how keyboard layout works. Even if the user changes the layout in a legitimate way, they are set per application. When you switch between application (activate one or another), the layout switched for this application most recently is used. But what you want to do not only contradicts to these order, it is not legitimate. It you could do it, it would be so intrusive that it could really break the functionality of the application. You should not even play with the idea of doing something like that. Even legitimate switch of available correctly installed input languages or layout is not a business of your application. You have to leave it to your users.

—SA
 
Share this answer
 
Comments
Soubarna Das 10-May-16 10:07am    
This is a valid keyboard definition file and we use it very often to type in our language and fonts.
Windows doesn't support all of our regional languages and fonts. And all those supported languages are in Unicode by default in windows. But some application like Adobe Photoshop doesn't support Unicode, so we use some of the application that can change the keyboard layout for some particular fonts of our language.
So my aim is to build an application by which user can change keyboard layout of his choice for some particular fonts. We have some commercial software like Swarno Type Manager or Anu Script Manager or Shrilipi that use that kind of keyboard definition file.
Sergey Alexandrovich Kryukov 10-May-16 10:42am    
Sorry, I have no idea what is that "keyboard definition"; I looked at the file and did not recognize what is that. I know how to create a keyboard layout for Windows. No, Windows does support all regional features, potentially; you just need to setup everything properly. Yes, sometimes the extra fonts are required, so you can supply them with your application. I also know that by the keyboard layouts supplied with Windows can be totally unsuitable. That's why I use my own and advise everyone to do the same when it is required.

But it does not mean that you need to use some special application used to change keyboard layout. What you describe is simply not legitimate Windows way. I also don't see a problem. Supply one or more keyboard layouts and let the users install and use them; that's all.

The whole idea "change keyboard layout for some particular fonts" suggests that you don't understand some basics about Unicode, fonts and keyboard. There is no such thing as "for font". An application does not have a font. And so on... Input methods have to be independent from font. It all works not as you may imagine.

—SA
Soubarna Das 10-May-16 11:11am    
Yes, I can understand your view. And may be I am wrong also. But we do have that kind of commercial applications that comes with some specific fonts and user can change the keyboard layout with them. Those software has no other use other than changing the keyboard layout. They don't even have any text editor with them. And those fonts are completely useless without those applications.
I am a newbie in VB programing and don't have much experience with the code.
But that kind of applications are very much popular in our region and I try to make my own to gather some knowledge on it.
You may visit their website below for more information
www.devinfotech.in/product.php
If you want I may pm you one. I just try to understand the coding and the logic behind that kind of application.
It will be very helpful for me if you give me some idea on how can do the coding of such application.
Sergey Alexandrovich Kryukov 10-May-16 11:26am    
Even if so, why "globally"? What you describe here is not globally, but per one particular application.
Yes, you can force the keyboard processing for your particular application, but it has nothing to do with what you asked initially.

Even in this case, I would strongly advise to use the legitimate approach I described: supply some keyboard layouts of your own and offer them as separate product. If you enforce some particular way of input even for one particular application (specifically in terms of text input, of course), you essentially take out important freedom from the user. If the user seriously rely on your commercial application, this limitation can even become a matter of legal litigation; there is a number of cases.

So, if you don't want to listen to a good advice (however, I'm not sure you understand it), you decide. But then, it has nothing to do with your initial question. In your particular application, you can completely bypass all language-related processing of the keyboard performed by the system and use keyboard directly. Well, not quite directly, not on the very fist hardware level (scan codes), but via handling of each key at the level of KeyDown/KeyUp event, which gives you Windows virtual keys. Making a local per-application keyboard layout based on this technique will be a fairly trivial task. What's the problem?

By the way, your "those fonts are completely useless without those applications" suggests that you don't understand how things work. You enter not fonts, you enter a Unicode character, but it's rendering depends on the font. You can enter any character in any general-purpose input control at anytime. Entering is totally unrelated to the fonts. And you don't need any application software to enter it. However, it might be not so easy to enter it immediately from the keyboard. But you can easily make it possible by crafting and installing some (alternative) keyboard layout module. Again, this is not application software.

—SA
Soubarna Das 10-May-16 12:56pm    
Applications which I mentioned changes keyboard layout globally and then you have to select the fonts supplied with them to type in any other application (e.g. MS Word, Photoshop etc.). I don't know if there is any legal issue or not.
When I started the project my initial approach was to build my desired layouts with Microsoft Keyboard Layout creator and registered those dlls at the time of installation and give the users a choice to choose their own layout through my application. But I revert to the idea as in this method user will have to use Unicode fonts and those are not supported in some applications like Adobe Photoshop.

I planned to include features in my application as follows:
1. User can select keyboard layouts supplied by me through my application
2. User can then use that layout to type in any text editor(MS word, Adobe Pagemaker etce.) or other applications (e.g. Adobe Photoshop)

I obviously follow your suggestions.
Soubarna Das wrote:

I solved out everything by following your advice. I have a question on that topic. Is it possible to add short-cut keys (Key sequence) for a particular keyboard programmatically via VB without going to control panel? Generally we can set key sequences by going to Control Panel -> Region And Languages -> Change Keyboards -> Advanced Key Settings -> Change Key Sequence.



Can you explain the both per-application and via RegistryHotkey.
Sure. But this is a separate question, not much related to your original issues.

Let's start with application level. This is simple.

First of all, you need to understand that keys are processed in several stages. Keyboard "knows nothing" about characters or virtual keys, it produces scan codes. If you handle something like KeyUp or KeyDown, you deal with virtual keys. The event come to your application before something is interpreted as characters, that is, before keyboard layout.

So, let's say, you have to handle if Ctrl+Shift+O is dispatched to some form. It will happen if you press on the key where the Latin O is written, even if the character is different for some alternative keyboard layout. In other words, even if in the current keyboard layout, pressing this key produces Greek Ω, you still need to check up for the virtual key System.Windows.Forms.Keys.O.

If you need it application-wide, you can do it for all forms. I can be something like:
VB
Public Partial Class MainForm ' main part derived from Form

    Private Sub Setup()
       Me.KeyPreview = True ' Important!
       Me.KeyDown += Function(sender, eventArgs) 
           If eventArgs.Modifiers =
                 (System.Windows.Forms.Keys.Control
                  Or System.Windows.Forms.Keys.Shift) AndAlso
                  eventArgs.KeyCode = System.Windows.Forms.Keys.O Then
                     ActivateSomething ' do whatever you want
           End If
        End Function
    End Sub 'Setup

End Class 'MainForm
' Translated from C# by Telerik, http://converter.telerik.com


In this code sample, your form will set up with this processing if you call Setup somewhere from the constructor. Note that KeyPreview is important because the keyboard focus is usually taken by some control, and you don't want this control to interfere with your hot key; the hot key should always be handled.

Same story goes with shortcut properties, such as, say, ToolStripMenuItem.ShortcutKeys. If you set Ctrl+Shift+O, it will be the key corresponding to Latin O, not the character according to keyboard layout. You can use menu items as application shortcuts.

In contrast to that, if you prefix, say, menu or button text with '&', next key will be underscored and work as accelerator. In this technique, you need to use Alt+O, where O should be real Latin O. The change of keyboard layout will disable this key, if it's not there in the keyboard layout.

That's all about application-local handling. Next step will be about the system-global.

—SA
 
Share this answer
 
v5
Comments
Soubarna Das 20-May-16 23:23pm    
Thank you very much Sergey... for the great help.
Sergey Alexandrovich Kryukov 20-May-16 23:25pm    
You are very welcome. Do you have problems with Solution 4?
—SA
Soubarna Das 21-May-16 5:01am    
Not at all. It works like a charm. Thank you very much for the great help.
Sergey Alexandrovich Kryukov 21-May-16 9:03am    
Great.
You are very welcome.
Good luck, call again.
—SA
[Continuing from Solution 3…]

To use global hot keys, you have to work with native Windows API, via P/Invoke:
RegisterHotKey function (Windows)[^],
UnregisterHotKey function (Windows)[^].

All the P/Invoke work is already done for you:
http://www.pinvoke.net/default.aspx/user32.registerhotkey[^],
http://www.pinvoke.net/default.aspx/user32.unregisterhotkey[^].

You can dispatch a registered event to your window function of your application's main form (for example). To handle it, you will need to override DefWndProc and handle the message WM_HOTKEY.

This technique also works with virtual keys, not characters, so you don't need to worry about a current keyboard layout. If it's Ctrl+Shift+O, it will be the key corresponding to Latin 'O', even if it's not in the keyboard layout. Isn't that nice?

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900