键盘事件响应

 键盘事件响应

1、创建基于单文档工程:KeyInput

2、在CKeyInputView中添加成员函数:

public:

bool m_bShiftDown;

CPoint m_ptCharacter;

3、CKeyInputView()中初始化:

m_bShiftDown = ture;

m_ptCharacter.x = 0;

m_ptCharacter.y = 0;

4、CKeyInputView()中添加消息响应函数:

WM_KEYDOWN  、 WM_CHAR  、WM_SETFOCUS

5、OnKeyDown中:

if(nChar == VK_SHIFT)

{

    m_bShiftDown = m_bShiftDown ? false:ture;

}

6、OnSetFocus中:

CreateSolidCaret(3,18);//建立光标

    SetCaretPos(m_ptCharacter);//设定光标位置

    ShowCaret();//显示光标

7、OnChar中:

if(m_bShiftDown)

    {

       if(nChar == 13)//回车

       {

           m_ptCharacter.x = 0;

           m_ptCharacter.y += 25;

           SetCaretPos(m_ptCharacter);

           ShowCaret();

       }else{

           CClientDC dc(this);

           HideCaret();

           dc.TextOut(m_ptCharacter.x,m_ptCharacter.y,

(LPCTSTR)&nChar);

           CSize textsize;

           textsize = dc.GetTextExtent((LPCTSTR)&nChar);

           m_ptCharacter.x += textsize.cx;

           SetCaretPos(m_ptCharacter);

           ShowCaret();

       }

    }

 

注:

GetDlgItem(IDC_INPUT)->SetWindowText(“模拟输入”);

GetDlgItem(IDC_INPUT)->SetWindowText(“停止输入”);

 

SetTimer(1,500,NULL);

OnTimer(){}

你可能感兴趣的:(事件)