检测C ++中按下事件时的Enter / Return [英] Detecting Enter/Return on Keydown event in C++

查看:370
本文介绍了检测C ++中按下事件时的Enter / Return的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果按下 Enter / 返回按钮,我将尝试在我的应用程序中检测。我的问题是 LVN_KEYDOWN 事件(表示已按下某个键)未检测到 Enter / 返回键。



我已经看过其他语言的类似问题,但找不到C ++的解决方案。



我的事件读取键新闻是:

  void ListOption :: OnLvnKeydownList1(NMHDR * pNMHDR,LRESULT * pResult)
{
LPNMLVKEYDOWN pLVKeyDow = reinterpret_cast< LPNMLVKEYDOWN>(pNMHDR);
// TODO:在这里添加您的控件通知处理程序代码
if(pLVKeyDow-> wVKey == VK_RETURN)
{
OnItemActivateList1(pNMHDR,pResult);
* pResult = 1;
}
* pResult = 0;
}

此代码适用于几乎任何键, / kbd>键。



我的对话框只有一个按钮,它的默认按钮值为FALSE。

更新:我的应用程序使用模态对话框..它包含一个CImageSheet,其中包含CImagePages(制表符) 。这里是一个图片,以便更好地解释(我已放置灰色块隐藏一些私人数据)。



当我按下 Enter 时,我想打开一个新的对话框,选项。目前这是通过 LVN_ITEMCTIVATE 事件(当用户双击某个项目时)完成的:

解决方案

您可以在拥有ListView的窗口中覆盖 PreTranslateMessage 。在这种情况下,它似乎是一个 CPropertyPage

  BOOL CMyPropertyPage :: PreTranslateMessage(MSG * pMsg)
{
//可选:键只有当ListView有焦点
if(GetFocus()==& List)
if(pMsg-> message == WM_KEYDOWN)
{
if(pMsg-> ; wParam == VK_RETURN)
{
//返回1去吃消息,或允许默认处理
int sel = List.GetNextItem(-1,LVNI_SELECTED);
if(sel> = 0)
{
MessageBox(VK_RETURN);
TRACE(ListView_GetNextItem%d\\\
,sel);
return 1;
}
else
TRACE(未选择ListView_GetNextItem,%d \\\
,sel);
}

if(pMsg-> wParam == VK_ESCAPE)
{
// do nothing!
}
}

return CPropertyPage :: PreTranslateMessage(pMsg);
}


I am trying to detect in my application, if the Enter/Return buttons are pressed. My problem is that the LVN_KEYDOWN event (Indicates that a key has been pressed) does not detect the Enter/Return key.

I have seen similar questions for other languages, but can not find a solution for C++.

My event to read the key press is:

void ListOption::OnLvnKeydownList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLVKEYDOWN pLVKeyDow = reinterpret_cast<LPNMLVKEYDOWN>(pNMHDR);
    // TODO: Add your control notification handler code here
    if(pLVKeyDow->wVKey == VK_RETURN)
    {
        OnItemActivateList1(pNMHDR, pResult);
        *pResult = 1;
    }
    *pResult = 0;
}

This code works for almost any key, execept for the Enter key.

My dialog has only one button, and it's "Default Button" value is FALSE. How is it possible to detect the keypress?

Update: My application uses modal dialogs.. It contains a CImageSheet that contains CImagePages(tabs). Here is an image to explain better (I have placed grey blocks to hide some private data).

When I press Enter, I wish to open a new dialog to change the option. Currently this is done with the LVN_ITEMCTIVATE event (when the user double clicks an item):

解决方案

You can override PreTranslateMessage in the window which owns the ListView. In this case it seems to be a CPropertyPage.

BOOL CMyPropertyPage::PreTranslateMessage(MSG* pMsg)
{
    //optional: you can handle keys only when ListView has focus
    if (GetFocus() == &List) 
    if (pMsg->message == WM_KEYDOWN)
    {
      if (pMsg->wParam == VK_RETURN)
      {
         //return 1 to eat the message, or allow for default processing
         int sel = List.GetNextItem(-1, LVNI_SELECTED);
         if (sel >= 0)
         {
            MessageBox("VK_RETURN");
            TRACE("ListView_GetNextItem %d\n", sel);
            return 1;
         }
         else
            TRACE("ListView_GetNextItem not-selected, %d\n", sel);
      }

      if (pMsg->wParam == VK_ESCAPE)
      {
         //do nothing!
      }
    }

    return CPropertyPage::PreTranslateMessage(pMsg);
}

这篇关于检测C ++中按下事件时的Enter / Return的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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