Click to See Complete Forum and Search --> : Keyboards - Is there a way?
Tom_D
November 29th, 2009, 12:52 AM
I am building a word processor in C# which uses Hebrew, Greek, and English. The context is C# with a VS 2008 RichTextBox. The user may switch back and forth between any of the 3 languages. At present I am uploading key assignments from an XML file into a SortedDictionary. I then use key events and ProcessCmdKey() to intercept events, and then SendKeys() to replace the keyCode with the dictionary code (ie. Hebrew key assignments, etc.). I find this all rather an ugly way to reassign the keyboard, so I'm looking for a better solution. I don't really want to simply start up the editor and then choose a language from the language bar. It must be internal to the program, and easily changeable to one of the other 2 from within.
As I said, I have a working solution right now, but I think it is rather crude. I am new to C# and .Net, so this may well be simply a statement of my naiveté. but I sure would appreciate some feedback on this. I have been poking around in the Registry, and it seems like the solution is right there, but I'm not sure just how to tie into the stored keyboards in the sense of assigning them to the physical keyboard for the duration of the session - or if such a thing is even possible.
Love to hear some comment.
Arjay
November 29th, 2009, 03:41 PM
You might check out the CultureInfo (http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx)class in msdn. Then look at the Thread CurrentCulture and CurrentUICulture properties. I know this will change the display and number formats, but not sure if it alters the keyboard mappings.
Tom_D
November 29th, 2009, 11:21 PM
Thanks for the suggestion. I did look follow up and look and down through this class. I loaded the sample into the IDE for a closer look at the class elements, but I could not find any member or method that seemed to get me close to what I want. The methods seem either to provide information only or else to support very localized changes (data/time etc.) - nothing that would permit full scale replacement of keyboard characters.
Tom_D
November 30th, 2009, 03:48 AM
Thanks for the suggestion. I did look follow up and look and down through this class.
That would be "I did follow up and scrolled up and down through the class." (How come the typos always hide until AFTER the "Submit" event?? )
Actually, I think I am now hot on the trail of my solution. The GetKeyboardLayoutList and ActivateKeyboardLayout look like the tools I need. I will follow through Michael Kaplan's very helpful article (http://beta.blogs.msdn.com/michkap/archive/2006/03/23/558658.aspx) and see if it shows me the way home.
Tom_D
November 30th, 2009, 04:28 PM
Ah - a simple solution after days of googling - where I found a thousand things I didn't want or need. The googling, of course, is but the direct result of my c# and .NET inexperience - and ultimately is a rather necessary (if exacerbating at times!!) way to learn. I include a very simple example below - with detail for newbies like me - of how to change your keyboard to another language during a windows forms session.
If one wants to gain direct control over all every possibility in the keyboard layout, then one will no doubt end up in with a PInvoke setup (see http://beta.blogs.msdn.com/michkap/Default.aspx?p=129). But for a simple change of languages, the code below works just fine - and especially so since one can use the free Microsoft Keyboard Layout Creator to build however many new keyboard(s) one wishes to have available.
For the demo below I simply set up a C# Windows Forms project in the VS 2008 IDE, added a rich text box and a list box to the form, and a SelectedIndexChanged event for the listbox ( double click on the listbox for the simplest way to do this), and then inserted the code below.
The form constructor populates the list box with the available input languages, and then the user can select from that list in the listbox. Just choose your language in the listBox, and then you will be typing in that language in the richTextBox.
To add keyboards/languages to your selection you can install the free Microsoft Keyboard Layout Creator, found here - http://msdn.microsoft.com/en-us/goglobal/bb964665.aspx
Of course if one wishes to change the language of controls etc (I don't) then there is a bit more work to do. Here is a good place to start - http://www.codeproject.com/KB/cs/formlanguageswitch.aspx
Hope this helps someone.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
listBox1.Items.Add(lang.LayoutName.ToString());
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//InputLanguage newlang = null;
this.Text = listBox1.SelectedItem.ToString();
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.LayoutName.ToString() == listBox1.SelectedItem.ToString())
{
InputLanguage.CurrentInputLanguage = lang;
}
}
}
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.