Click here to Skip to main content
15,884,537 members
Articles / Desktop Programming / MFC
Article

Language Input Mode (mainly for hebrew programmers)

Rate me:
Please Sign up or sign in to vote.
2.94/5 (15 votes)
10 May 20032 min read 117.3K   1.6K   21   26
How to adjust the input in regard to the desired language mode

Introduction

<h0>In my latest project - "Cabnet", I have implemented the language input mode keyboard for restricting the user just to Hebrew typing - the trick isn't to prevent unwanted letters but rather to adjust the keyboard at need.

Image 1

Details

Developers Target

As the title stated, this article is for mainly Hebrew developers. But in fact, any development which deal with multi-language issue, where the user has to press on Alt+Shift && Caps Lock for language mode input movement, is a legitimate client for the following lines. So, the title is no more than a misled road sign. Then why I did it that way? Local-patriotism? Maybe. But - listen - while most of the time Hebrew programmers must use English as communication tool - for one time only the whole world has to be a hebrewphile but hopefully not a hebrewphobe to read this article.

The problem

As a writer of many client applications - I used to be frustrated from the frequently necessity to change the keyboard input mode from Hebrew to English and vice versa.

Cause

While, usually it is useful to have this option - many times it is just irritating NEEDLESSLY.

Example

Suppose the user has to type his name on edit box and the app's demand is ONLY for Hebrew (or other local language) form.

Solution

  1. Prevent unwanted letters.
  2. Automatically change the keyboard attribute to Hebrew input mode.
You could implement the first solution but it's more wise to fulfill the second approach - as I did.

Demo's Features

void SetEnglishLanguage()
void SetLanguageDynamic()
void SetLocalLanguage()

How it works ?

As you probably know, the windows loop messaging contain three function:
GetMessage()
TranslateMessage()
DispatchMessage()
One role of the TranslateMessage() function is to scan the state of the controls keys - because those states are the trigger factors for manipulation messages.

So, the trick is to hook the message before the translation process - if it is necessary, adjust the language mode by requesting the currently language setting and deactivate the caps lock - thus finally you could launch the built-in translation.

Snippet of code

BOOL CHebrewEdit::PreTranslateMessage(MSG* pMsg) 
{
    switch(pMsg->message) {

    case WM_KEYDOWN:

      ValidateLanguage();
    
    break;
    default:
    break;
};
    return CEdit::PreTranslateMessage(pMsg);
}
Note : the CHebrewEdit name could be replaced by 'CArabicEdit', 'CChineseEdit' or for global name solution 'CLocalEdit'.

void CHebrewEdit::ValidateLanguage()
{
    int ks;
    
    BOOL caps;

    if (IsLanguageDynamic()) return;
    
    if (m_nLanguage == LANG_M_LOCAL)
        
              SendMessage(WM_INPUTLANGCHANGEREQUEST,5,0x40D040D);
        else
    
    if (m_nLanguage == LANG_M_ENGLISH) {
        
              SendMessage(WM_INPUTLANGCHANGEREQUEST,5,0x4090409);
    }

        else ASSERT(FALSE);

        caps = (IsCapsSet() & 0x01);
    
    if (IsEnglishLanguage()) caps=!caps;
    

    if (caps) { // for capslock
        
    keybd_event(VK_CAPITAL,0,0,0);
    keybd_event(VK_CAPITAL,0,KEYEVENTF_KEYUP,0);

    }
    

}

Source Code

My source code implements only the edit box extension, although the code is simple and portable for other windows controls, or even for the whole process by hooking the input process, although it isn't much recommend if you eager to reach speed optimum and robustness (see: SetWindowsHookEx).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
I'm just a small brain connected to an un-limited
brains network, which lead directly to the top ten
giant minds. Which lead me to the
thought : who am i?

I am just a jerry-built god !

Comments and Discussions

 
GeneralMSDN sample Pin
Fisico5-Dec-05 21:03
Fisico5-Dec-05 21:03 
QuestionSimpler Method Pin
shmulyeng15-Nov-05 10:48
shmulyeng15-Nov-05 10:48 
AnswerRe: Simpler Method Pin
Senthil Kumar U18-Jul-06 22:35
Senthil Kumar U18-Jul-06 22:35 
AnswerRe: Simpler Method Pin
Nader Al Khatib1-Jul-07 11:10
Nader Al Khatib1-Jul-07 11:10 
GeneralMake Dos to Win Pin
Y_R27-Mar-05 10:57
Y_R27-Mar-05 10:57 
GeneralNeed help Pin
percyvimal25-Nov-03 21:08
percyvimal25-Nov-03 21:08 
GeneralActivateKeyboardLayout( ) Pin
Anonymous14-Oct-03 23:13
Anonymous14-Oct-03 23:13 
GeneralThe Best Solution For This Matter Pin
Yoavf6-Oct-03 23:09
Yoavf6-Oct-03 23:09 
GeneralTo moshe masas &amp; Boaz72 and to all Pin
Yoavf6-Oct-03 22:56
Yoavf6-Oct-03 22:56 
GeneralRe: To moshe masas &amp; Boaz72 and to all Pin
Anonymous13-Oct-03 21:02
Anonymous13-Oct-03 21:02 
GeneralRe: To moshe masas & Boaz72 and to all Pin
moshe masas4-Nov-03 3:38
moshe masas4-Nov-03 3:38 
GeneralRe: To moshe masas &amp; Boaz72 and to all Pin
Anonymous6-Nov-03 4:34
Anonymous6-Nov-03 4:34 
GeneralAlternative Solution Pin
Member 52199513-Aug-03 23:48
Member 52199513-Aug-03 23:48 
GeneralRe: Alternative Solution Pin
moshe masas31-Aug-03 0:36
moshe masas31-Aug-03 0:36 
GeneralRe: Alternative Solution Pin
boaz7231-Aug-03 8:55
sussboaz7231-Aug-03 8:55 
GeneralRe: Alternative Solution Pin
moshe masas2-Sep-03 3:31
moshe masas2-Sep-03 3:31 
GeneralNot working Pin
Didaa14-May-03 19:27
Didaa14-May-03 19:27 
GeneralRe: Not working Pin
moshe masas15-May-03 1:45
moshe masas15-May-03 1:45 
GeneralRe: Not working Pin
Didaa15-May-03 4:26
Didaa15-May-03 4:26 
GeneralRe: Not working Pin
moshe masas15-May-03 5:22
moshe masas15-May-03 5:22 
GeneralRe: Not working Pin
Anonymous16-May-03 3:58
Anonymous16-May-03 3:58 
GeneralRe: Not working Pin
moshe masas19-May-03 4:39
moshe masas19-May-03 4:39 
General&#1508;&#1512;&#1493;&#1497;&#1497;&#1511;&#1496; &#1502;&#1510;&#1495;&#1497;&#1511; Pin
Bengi12-May-03 1:11
Bengi12-May-03 1:11 
Generalcabnet ?!? is this a joke !?! Pin
moshe masas12-May-03 3:09
moshe masas12-May-03 3:09 
GeneralRe: cabnet ?!? is this a joke !?! Pin
Bengi12-May-03 9:06
Bengi12-May-03 9:06 
LOL
no problem =)
thnx & toda hehe

/- Bengi - \

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.