自己写了一个软键盘程序,是通过keybd_event模拟发送软键盘消息的,可是编辑框却收不到软键盘的输入,但编辑框所在的对话框却可以接收到象回车键、退出键等消息,所以我想是不是编辑框没有输入焦点造成的,可是同样的代码在模拟器上却可以实现输入,在三星arm92440板子上就不可以了,这真是奇怪了,希望高手帮帮我,多谢了。
主要代码:
HWND m_hFocus;
CKeyboardDlg *pCharKeyboardDlg;
CNumKeyboardDlg *pNumKeyboardDlg;
/////////////////////////////////////////////////////////////////////////////
// CKeyboardDlg dialog
/////////////////////////////////////////////////////////////////////////////
// CKeyboardDlg message handlers
BOOL CKeyboardDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
pCharKeyboardDlg = this;
pNumKeyboardDlg = new CNumKeyboardDlg;
pNumKeyboardDlg->Create(IDD_NUM_KEYBOARD_DLG, this);
//设置矩形窗口(内部)
CRect winrect;
CRgn rgn;
// CBrush brush;
// brush.CreateSolidBrush(RGB(255,255,255));
GetClientRect(&winrect);
// rgn.CreateRectRgn(0,0,winrect.right,winrect.bottom);
// SetWindowRgn(rgn, 1);
// SetWindowText(_T("软键盘"));
// GetWindowRect(winrect);
::SetWindowPos(this->m_hWnd, HWND_TOPMOST, 60, 320, winrect.Width(), winrect.Height() ,SWP_NOSIZE); //必须的
// MoveWindow(160, 400, winrect.Width(), winrect.Height(), true);
keybd_event(VK_CAPITAL, 0, 0, 0);
SHORT ntmp = GetKeyState(VK_CAPITAL); //只能得到VK_CAPITAL 的触发状态,0为大写状态,1为小写状态
keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
if(ntmp == 1)
{
keybd_event(VK_CAPITAL, 0, 0, 0);
keybd_event(VK_CAPITAL, 0, KEYEVENTF_KEYUP, 0);
}
m_KeyS.Attach(IDC_KEY_S, this);
m_KeyS.SetWindowText(_T("S"));
m_KeyRParenthesis.Attach(IDC_KEY_RIGHT_PARENTHESIS, this);
m_KeyRParenthesis.SetWindowText(_T(")"));
m_KeyReturn.Attach(IDC_KEY_RETURN, this);
m_KeyReturn.SetWindowText(_T("Return"));
SetWindowPos(&wndTopMost, 0, 0, 0, 0, //把窗口放到最顶端
SWP_NOSIZE | SWP_NOMOVE);
::SetProp(m_hWnd, AfxGetApp()->m_pszExeName, (HANDLE)1);
return TRUE; // return TRUE unless you set the focus to a control
}
int CKeyboardDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
//置窗体为最顶层显示及其位置
CRect rcW;
GetWindowRect(&rcW);
SetWindowPos(&wndTopMost, rcW.left, rcW.top, rcW.Width(), rcW.Height(), SWP_NOSIZE);
return 0;
}
void CKeyboardDlg::ReleaseFocus()
{
//释放焦点
if(IsWindow(m_hFocus))
{
HWND wnd = ::GetForegroundWindow();
if(IsWindow(wnd))
{
if(wnd == m_hFocus)
{
return;
}
}
//设置保存的焦点窗口处于激活状态
::SetForegroundWindow(m_hFocus);
//::SetActiveWindow(m_hFocus);
//::SetFocus(m_hFocus);
}
}
void CKeyboardDlg::OnKeyA()
{
// TODO: Add your control notification handler code here
BYTE key = 'A';
SendKeyMessage(key);
}
BOOL CKeyboardDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
HWND wnd = ::GetForegroundWindow();
if(IsWindow(wnd))
{
//如果当前激活窗口不是本程序的窗口
if(wnd != this->m_hWnd)
{
//激活窗口发生了改变
if(m_hFocus != wnd)
{
/* if(IsWindow(m_hFocus))
{
//释放本进程和gFocus线程的联系
// AttachThreadInput(
// GetWindowThreadProcessId(m_hWnd,NULL),
// GetWindowThreadProcessId(m_hFocus,NULL),
// FALSE);
m_hFocus = ::GetFocus();
}
*/
m_hFocus = ::GetFocus();
m_hFocus = wnd;
//使本进程和激活窗口的进程联系起来,接收本进程的按键消息
// AttachThreadInput(
// GetWindowThreadProcessId(m_hWnd,NULL),
// GetWindowThreadProcessId(m_hFocus,NULL),
// TRUE);
}
}
}
if(pMsg->message == WM_CHAR)
RETAILMSG(1, (TEXT("HTFurnace::message = %d, wParam = %d, nTmp = %d, m_hFocus = %d\r\n"), pMsg->message, pMsg->wParam, pMsg->lParam, m_hFocus));
return CDialog::PreTranslateMessage(pMsg);
}
int CKeyboardDlg::DescribeKeyState()
{
int state = 0;
short ks;
ks = GetKeyState(VK_CAPITAL);//返回值小于0,则被触发,大于0没有被按下
if(ks & 0x000F) state += 0x01;
ks = GetKeyState(VK_SHIFT);
if(ks & 0xF000) state += 0x02;
ks = GetKeyState(VK_CONTROL);
if(ks & 0xF000) state += 0x04;
return state;
}
void CKeyboardDlg::OnKeyZ()
{
// TODO: Add your control notification handler code here
BYTE key = 'Z';
SendKeyMessage(key);
}
void CKeyboardDlg::OnSwitchtonum()
{
// TODO: Add your control notification handler code here
pNumKeyboardDlg->ShowWindow(SW_SHOW);
this->ShowWindow(SW_HIDE);
// pNumKeyboardDlg = new CNumKeyboardDlg;
// pNumKeyboardDlg->DoModal();
}
void CKeyboardDlg::OnKeyReturn()
{
// TODO: Add your control notification handler code here
SendKeyMessage(VK_RETURN);
}
void CKeyboardDlg::OnKeyEnter()
{
// TODO: Add your control notification handler code here
SendKeyMessage(VK_RETURN);
}
void CKeyboardDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_OldPoint = point;
SetCapture();//调用SetCapture后,即使鼠标移动出客户区,你也可以接受到鼠标消息
//CDialog::OnLButtonDown(nFlags, point);
}
void CKeyboardDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CRect winrect;
GetWindowRect(&winrect);
// RETAILMSG(1, (TEXT("OnLButtonUp::left = %d, top = %d\r\n"), winrect.left, winrect.top));
if((point.x != m_OldPoint.x) || (point.y != m_OldPoint.y))
{//移动
int offset_x = point.x - m_OldPoint.x;
int offset_y = point.y - m_OldPoint.y;
MoveWindow(winrect.left + offset_x, winrect.top + offset_y, winrect.Width(), winrect.Height(), true);
}
ReleaseCapture();
//CDialog::OnLButtonUp(nFlags, point);
}
void CKeyboardDlg::SendKeyMessage(int vk_code)
{
if(IsWindow(m_hFocus))
{
::SetActiveWindow(m_hFocus);
}
ReleaseFocus();
if(vk_code >= 'A' && vk_code <= 'Z')
{
/* SHORT nTmp = GetAsyncKeyState(VK_CAPITAL);
if(!(GetKeyState(VK_CAPITAL) && 0xFF))
if(nTmp < 0)
{
keybd_event(VK_CAPITAL,0,0,0);
keybd_event(VK_CAPITAL,0,KEYEVENTF_KEYUP,0);
}
*/
if(m_bPressKeyShift)
keybd_event(VK_SHIFT, 0, 0, 0);
if(!m_bCapsLK)
vk_code += 32;//lowercase
keybd_event(vk_code, 0, 0, 0);
keybd_event(vk_code, 0, KEYEVENTF_KEYUP, 0);
if(m_bPressKeyShift)
{
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
m_bPressKeyShift = FALSE;
m_KeyShift.ChangeColor(WHITE_COLOR);
GetDlgItem(IDC_KEY_SHIFT)->SetWindowText(_T("SHIFT"));
}
}
else if(vk_code == VK_LPARENTHESIS || vk_code == VK_RPARENTHESIS)
{
keybd_event(VK_SHIFT , 0, 0, 0);
keybd_event(vk_code, 0, 0, 0);
keybd_event(vk_code, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SHIFT , 0, KEYEVENTF_KEYUP, 0);
}
else if(vk_code == VK_SHIFT)
{
keybd_event(VK_SHIFT, 0, 0, 0);
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
m_KeyShift.ChangeColor(PALEGREEN_COLOR);
m_bPressKeyShift = TRUE;
//m_KeyShift.
}
else if(vk_code == VK_CAPITAL)
{
keybd_event(vk_code, 0, 0, 0);
SHORT ntmp = GetKeyState(VK_CAPITAL); //只能得到VK_CAPITAL 的触发状态,0为大写状态,1为小写状态
keybd_event(vk_code, 0, KEYEVENTF_KEYUP, 0);
char text;
CString strText;
if(ntmp == 0)
{
m_bCapsLK = TRUE;
for(int id = IDC_KEY_A; id <= IDC_KEY_Z; id++)
{
text = 'A' + (id - IDC_KEY_A);
strText = text;
GetDlgItem(id)->SetWindowText(strText);
}
}
else if(ntmp == 1)
{
m_bCapsLK = FALSE;
for(int id = IDC_KEY_A; id <= IDC_KEY_Z; id++)
{
text = 'A' + (id - IDC_KEY_A) + 32;
strText = text;
GetDlgItem(id)->SetWindowText(strText);
}
}
}
else//VK_BACK,,VK_COMMA,VK_PERIOD,VK_ESCAPE,VK_SEMICOLON,VK_SLASH,VK_SPACE,VK_RETURN
{
keybd_event(vk_code, 0, 0, 0);
keybd_event(vk_code, 0, KEYEVENTF_KEYUP, 0);
}
// ReleaseFocus();
}