在C ++ MFC应用程序中模拟应用程序内部的键盘输入 [英] Simulate keyboard input inside app in C++ MFC app

查看:439
本文介绍了在C ++ MFC应用程序中模拟应用程序内部的键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 2个编辑控件创建了一个对话框MainDialog.cpp,其ID为IDC_EDITCONTROL_AIDC_EDITCONTROL_B,并且变量分别定义为m_editControlAm_editControlB.

I created a dialog MainDialog.cpp with 2 edit controls whose IDs are IDC_EDITCONTROL_A and IDC_EDITCONTROL_B, and have variables defined as m_editControlA and m_editControlB, respectively.

此外,我还有 2个按钮,其ID为IDC_MFCBUTTON_KEY_XIDC_MFCBUTTON_KEY_Y,变量分别为m_buttonKeyXm_buttonKeyY.

Also, I have 2 buttons whose IDs are IDC_MFCBUTTON_KEY_X and IDC_MFCBUTTON_KEY_Y, and variables are m_buttonKeyX and m_buttonKeyY, respectively.

下面是源文件中的代码

#include "afxdialogex.h" 

IMPLEMENT_DYNAMIC(CMainDialog, CDialogEx)

CMainDialog::CMainDialog(CWnd* pParent): CDialogEx(IDD_MAIN_DIALOG, pParent)
{
}

CMainDialog::~CMainDialog()
{
}

void CMainDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_EDITCONTROL_A, m_editControlA);
    DDX_Control(pDX, IDC_EDITCONTROL_B, m_editControlB);
    DDX(Control(pDX, IDC_MFCBUTTON_KEY_X, m_buttonKeyX);
    DDX(Control(pDX, IDC_MFCBUTTON_KEY_Y, m_buttonKeyY);
}

BEGIN_MESSAGE_MAP(CMainDialog, CDialogEx)
    ON_EN_CHANGE(IDC_EDITCONTROL, &CMainDialog::OnEnChangeEditA)
    ON_BN_CLICKED(IDC_MFCBUTTON_KEY_X, &CMainDialog::OnBnClickedButtonX)
    ON_BN_CLICKED(IDC_MFCBUTTON_KEY_Y, &CMainDialog::OnBnClickedButtonY)
END_MESSAGE_MAP()

void CMainDialog::OnBnClickedButtonX()
{
    m_editControlA.SetWindowTextW(_T("X"));  // test
}

void CMainDialog::OnBnClickedButtonX()
{
    m_editControlA.SetWindowTextW(_T("Y"));  // test
}

我试图了解如何让每个按钮发送各自的字符(在此示例中为XY)(如果已选择),将其发送给所选的编辑控件 .本质上,我想模拟键盘输入.

I am trying to understand how I can have each button send their respective character (i.e. X or Y in this example) to the selected edit control if one is selected. Essentially, I would like to simulate keyboard input.

我已阅读有关如何模拟键盘事件以及上一个问题,我发现GetFocus会很有用,但我目前的主要问题仍然是发送输入.

I have read the docs about how to simulate keyboard events and also the sendMessage but I could not understand how to implement it since my C++ knowledge is very basic. Also, following my previous question I have found that the GetFocus would be useful but still my main issue currently is sending the input.

任何示例代码或有用的链接对于我学习如何在应用程序内部模拟键盘输入都是非常有用的.

Any example code or useful link could be very useful for me to learn how I can simulate a keyboard input inside an app.

推荐答案

使用WM_CHAR消息将字符从操作系统发送到编辑控件.

The characters are sent from the OS to the edit controls using the WM_CHAR message.

实际上它比这复杂一些,但是您不需要模拟整个WM_KEYUP WM_KEYDOWN消息序列,因为它的最终结果是生成WM_CHAR消息.

In reality it is a bit more complex than that, but you do not need to emulate the entire WM_KEYUP WM_KEYDOWN message sequence, since its end result is to generate a WM_CHAR message.

即使字符没有焦点,您也可以使用CWnd :: PostMessage将字符直接发送到您的编辑控件.

You can use CWnd::PostMessage to send characters directly to your edit controls, even when they do not have the focus.

您可能已经在这里找到WM_CHAR的文档: https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms646276(v = vs.85).aspx

You have probably already found the documentation for WM_CHAR here: https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms646276(v=vs.85).aspx

哎呀..对不起,我的法语,英语医生在这里 https://msdn.microsoft .com/en-us/library/windows/desktop/ms646276(v = vs.85).aspx

oops.. excuse my french, ths english doc is here https://msdn.microsoft.com/en-us/library/windows/desktop/ms646276(v=vs.85).aspx

(只需将fr-fr更改为en-us就可以了,它可能适用于所有其他语言,整齐!

(just changing the fr-fr to en-us does the trick, it probably works for all other languages, neat!

  • wParam 包含要发送的字符.可以是普通的ASCII字符,也可以是VK_常量之一...我建议您使用unicode版本WM_CHARW,因为当今大多数Windows软件都使用unicode.宽字符的符号为L'X'或_T('X'),Unicode(UTF-16)字符类型为wchar_t.

  • wParam holds the character you want to send. Either an plain ASCII character, or one of the VK_ constants... I suggest you use the unicode version WM_CHARW, as most windows software uses unicode nowadays. The notation for wide chars is either L'X' or _T('X'), the unicode (UTF-16) character type is wchar_t.

lParam 包含其他击键详细信息,0应该适合您想做的事情.

lParam contains other keystroke details, 0 should be fine for what you want to do.

发送X,只需致电

 m_editControlA.PostMessage(WM_CHAR, _T('X'));  

使用_T()表示法时,括号之间的字符(或字符串)文字将自动转换为应用程序unicode设置的正确字符宽度(您应将其设置为UNICODE,因为这是操作系统使用的语言) ,并且也是Windows CE的唯一有效编码,例如,您应该习惯于处理这种类型.

When using the _T() notation, the character (or string) literal between the parenthesis will be automatically converted to the right character width for your app's unicode setting (you should set that to UNICODE, since that's what the OS is using, and is also the only valid encoding for Windows CE, for example, and you should get used to manipulating this type.

几乎所有对字符串进行操作的C库函数的_T()宏和_t *替代都在tchar.h中定义,tchar.h包含在stdafx.h中的Visual Studio中.在MFC下,您将主要使用CString,但是很高兴知道这些东西在哪里.

the _T() macros and _t* overrides for almost all C library functions operating on strings are defined in tchar.h, which is included by Visual Studio in stdafx.h. Under MFC, you'll mostly use CString, but it's good to know where these things are.

运行该命令时,应开始使用WM_KEYDOWN进行播放.您会发现直接发送到对话框的PostMessage(WM_CHAR,VK_ESCAPE)不会将其关闭,而PostMessage(WM_KEYDOWN,VK_ESCAPE)则会将其关闭.并且该m_editBox.PostMessage(WM_KEYDOWN,_T('X'))将向您的编辑框发送一个较低的键'x'.但这是另一个要引起争议的话题.

When you get that running, you should start playing with WM_KEYDOWN. You will discover that PostMessage(WM_CHAR, VK_ESCAPE) directly to your dialog does not close it, while a PostMessage(WM_KEYDOWN, VK_ESCAPE) does. And that m_editBox.PostMessage(WM_KEYDOWN, _T('X')) will send a lower key 'x' to your edit box. But that's another topic to ivestigate.

使用MFC玩得开心!

最后一个问题:

当然可以,但是会变得更加复杂,因为单击按钮后您的按钮将获得焦点.您必须为每个编辑框创建EN_SETFOCUS的处理程序,并添加CWnd *数据成员以跟踪上一个具有焦点的编辑框.

Sure, but it gets a bit more complicated, as your button will gain focus, as soon as you click on it. You'd have to create handlers for EN_SETFOCUS for eeach of your edit boxes, and add a CWnd* data member to keep track of the last edit box that had focus.

您的EN_SETFOCUS处理程序应该看起来像这样

Your EN_SETFOCUS handlers should look something like this

void CdlgDlg::OnEnSetfocusEdit1()
{ 
    m_pWndLastFocus = &m_edit1;
}

请不要忘记在构造函数中将指针设置为NULL,并在调用m_pWndLastFocus->PostMessage()之前将其设置为有效.

Don't forget to set the pointer to NULL in your constructor and to chjeck it's valid before calling m_pWndLastFocus->PostMessage() though.

这篇关于在C ++ MFC应用程序中模拟应用程序内部的键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆