MFC OnKeyDown's UINT nChar vs KeyEventArgs in c#

Is there any way to get the equivaleny UINT nChar (used by CWnd's OnKeyDown method in MFC) from a KeyEventArgs instance in c#

Answer this question

MFC OnKeyDown's UINT nChar vs KeyEventArgs in c#

  • dustin1

    I don't know what kind of output you expect, but i have made a little example. The ToUnicode method, is that a custom method


    private void control_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    switch(e.KeyCode)
    {
    case Keys.NumPad0:
    case Keys.NumPad1:
    case Keys.NumPad2:
    case Keys.NumPad3:
    case Keys.NumPad4:
    case Keys.NumPad5:
    case Keys.NumPad6:
    case Keys.NumPad7:
    case Keys.NumPad8:
    case Keys.NumPad9:
    {
    char append = (char)(e.KeyCode - Keys.NumPad0);
    ascii_key = string.Format( "0{0}", append );
    }
    }
    }




  • Jenssa

    I have this method in a C++ method:

    void KeyboardHandlers::OnKeyDown(HBaseViewClr^ hView, UINT nChar, bool bAlt)
    {
    switch (nChar) {
    case VK_NUMPAD0:
    case VK_NUMPAD1:
    case VK_NUMPAD2:
    case VK_NUMPAD3:
    case VK_NUMPAD4:
    case VK_NUMPAD5:
    case VK_NUMPAD6:
    case VK_NUMPAD7:
    case VK_NUMPAD8:
    case VK_NUMPAD9:
    {
    ascii_key =
    '0' + nChar - VK_NUMPAD0;
    }
    break;

    default: {
    ToUnicode(nChar, MapVirtualKey(nChar, 0), keys, (LPWSTR)&ascii_key, 1,0);
    }
    }
    The KeyDown event is raised by a C# Windows Forms App.


  • Mats Upptrom

    You can use the KeyCode and cast that to a Char or you can use the KeyValue property. What is the problem your are trying to solve


  • MFC OnKeyDown's UINT nChar vs KeyEventArgs in c#