"AND" "OR"
Главная Информер Журнал Форум

Как программно переключить раскладку клавиатуры?

Используйте функцию ActivateKeyboardLayout (...), которая позволяет установить язык для текущего потока.

А вот пример на Visual C++ который показывает как использовать эту функцию
(прислал Осадчий Константин):

**********************************************************************

// NewWordDlg.cpp : implementation file
#include "stdafx.h"
#include "Diction.h"
#include "DictionSet.h"
#include "NewWordDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNewWordDlg dialog

CNewWordDlg::CNewWordDlg(CDictionSet* prs,CWnd* pParent /*=NULL*/)
                                      : CDialog(CNewWordDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNewWordDlg)
	m_strRus = _T("");
	m_strAngl = _T("");
	//}}AFX_DATA_INIT
	m_pSet = prs;
}

void CNewWordDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNewWordDlg)
	DDX_Control(pDX, IDCANCEL, m_ctrlCancel);
	DDX_Control(pDX, IDOK,     m_ctrlOk);
	DDX_Text(pDX, IDC_RUS,     m_strRus);
	DDX_Text(pDX, IDC_ANGL,    m_strAngl);
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_RUS,  m_pSet->m_RusWord);
	DDX_Text(pDX, IDC_ANGL, m_pSet->m_AnglWord);
}

BEGIN_MESSAGE_MAP(CNewWordDlg, CDialog)
	//{{AFX_MSG_MAP(CNewWordDlg)
	ON_EN_SETFOCUS(IDC_ANGL, OnSetfocusAngl)
	ON_EN_SETFOCUS(IDC_RUS, OnSetfocusRus)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
// CNewWordDlg message handlers
BOOL CNewWordDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	lstrcpy(m_szLangOld, _T("\0"));
	GetKeyboardLayoutName(m_szLangOld);
	return TRUE;
}


void CNewWordDlg::OnSetfocusAngl() 
{
	DWORD dwLang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
	CString strMl; strMl.Format(_T("00000%x"), dwLang);
	WORD mlwLang = LOWORD(dwLang);
	dwLang = MAKELONG(mlwLang, mlwLang);
	CString strLang; strLang.Format(_T("%x"), dwLang);
	TCHAR szLang[10];
	lstrcpy(szLang, _T("\0"));
	GetKeyboardLayoutName(szLang);
	if(strMl != szLang) {
		lstrcpy(szLang, strLang);
		ActivateKeyboardLayout(LoadKeyboardLayout(szLang, KLF_ACTIVATE |
                                                  KLF_REPLACELANG), KLF_REORDER);
	}
}	

//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
void CNewWordDlg::OnSetfocusRus() 
{	
	DWORD dwLang = MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT);
	CString strMl; strMl.Format(_T("00000%x"), dwLang);
	WORD mlwLang = LOWORD(dwLang);
	dwLang = MAKELONG(mlwLang, mlwLang);
	CString strLang; strLang.Format(_T("%x"), dwLang);
	TCHAR szLang[10]; 
	lstrcpy(szLang, _T("\0"));
	GetKeyboardLayoutName(szLang);
	if(strMl != szLang) {
		lstrcpy(szLang, strLang);
		ActivateKeyboardLayout(LoadKeyboardLayout(szLang,
                              KLF_ACTIVATE | KLF_REPLACELANG), KLF_REORDER);
	}
}

void CNewWordDlg::OnOK() 
{
	DWORD dwLang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
	WORD mlwLang = LOWORD(dwLang);
	dwLang = MAKELONG(mlwLang, mlwLang);
	CString strLang; strLang.Format(_T("%x"), dwLang);
	TCHAR szLang[10];
	lstrcpy(szLang, _T("\0"));
	GetKeyboardLayoutName(szLang);
	CString str = m_szLangOld;
	if(str != szLang) {
		lstrcpy(szLang, strLang);
		ActivateKeyboardLayout(LoadKeyboardLayout(szLang,
                           KLF_ACTIVATE | KLF_REPLACELANG), KLF_REORDER);
	}
	CDialog::OnOK();
}

void CNewWordDlg::OnCancel() 
{
	DWORD dwLang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
	WORD mlwLang = LOWORD(dwLang);
	dwLang = MAKELONG(mlwLang, mlwLang);
	CString strLang; strLang.Format(_T("%x"), dwLang);
	TCHAR szLang[10];
	lstrcpy(szLang, _T("\0"));
	GetKeyboardLayoutName(szLang);
	CString str = m_szLangOld;
	if(str != szLang) {
		lstrcpy(szLang, strLang);
		ActivateKeyboardLayout(LoadKeyboardLayout(szLang,
                             KLF_ACTIVATE | KLF_REPLACELANG), KLF_REORDER);
	}
	CDialog::OnCancel();
}