locked
Detect Multiple key pressed in Winrt/Metro app using c#. RRS feed

  • Question

  • Hi All,

    I am store app/WinRT developer. My question is how to detect multiple key press in WinRT/Merto app using c#.

    I have tried different messenger applications for Store app/Metro app now, and it seems that there are consensus that

    a "Plain Enter" will send the message
    a "Shift+Enter" will create a newline
    a "CTRL+Enter" does nothing
    a "ALT+Enter" does nothing

    We should conform to this consensus.

    That means. Enter should send the text and Shift+Enter should create a newline.

    How can I do this in Metro app using c#. I am also follow the native message app,their all the functionality are apply which I enclose above so I belief that it is possible.But how can I do this it is very importent for me.Please reply as early as possible.

    ThanQ

    Ganesh

    Thursday, November 14, 2013 6:48 AM

Answers

  • Hi Ganesh,

    you  just have to listen for the onkeyDown Event

    For example:
    following code was from this thread

    private void onKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Control) m_ControlPressed = true;
        else if (m_ControlPressed && e.Key == VirtualKey.V)
        {
            // do some stuff
        }
    }


    Also see here: http://stackoverflow.com/questions/1265634/keydown-recognizing-multiple-keys

    And here: http://msdn.microsoft.com/en-us/library/windows/apps/hh868246.aspx

    Andy


    • Edited by SW_Andy Thursday, November 14, 2013 9:51 AM
    • Marked as answer by Jamles HezModerator Friday, November 15, 2013 12:37 AM
    Thursday, November 14, 2013 9:49 AM
  • Hi All,

    At last  I am solving it code given bellow.

                     if (e.Handled)
                        return;
                    var enter = Window.Current.CoreWindow.GetKeyState(VirtualKey.Enter);
                    var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift);
                    if (enter.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down) && shift.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
                    {                   
                        txtMessage.Text += "\r\n";
                        txtMessage.SelectionStart = txtMessage.Text.Length;
                        e.Handled = true;
                    
                    }
                    else if (enter.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))             
                    {                
                    
                        e.Handled = true;                   
                    }
                }
               

    ThanQ

    Ganesh


    Thursday, November 14, 2013 12:32 PM

All replies

  • Hi Ganesh,

    you  just have to listen for the onkeyDown Event

    For example:
    following code was from this thread

    private void onKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Control) m_ControlPressed = true;
        else if (m_ControlPressed && e.Key == VirtualKey.V)
        {
            // do some stuff
        }
    }


    Also see here: http://stackoverflow.com/questions/1265634/keydown-recognizing-multiple-keys

    And here: http://msdn.microsoft.com/en-us/library/windows/apps/hh868246.aspx

    Andy


    • Edited by SW_Andy Thursday, November 14, 2013 9:51 AM
    • Marked as answer by Jamles HezModerator Friday, November 15, 2013 12:37 AM
    Thursday, November 14, 2013 9:49 AM
  • Hi SW_Andy,
    ThanQ for your valuable reply.I am solve it by using this code bellow

    var enter = Window.Current.CoreWindow.GetKeyState(VirtualKey.Enter);
                    var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift);
                    if (enter.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down) && shift.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
                    {
                                        
                    }
                    else if (enter.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))            
                
                    {                 

                    }

    But I am suffering another problem when we press shift+Enter we get a new line like native message in windows8.How can I do this?

    ThanQ

    Ganesh

    Thursday, November 14, 2013 10:05 AM
  • Hi All,

    At last  I am solving it code given bellow.

                     if (e.Handled)
                        return;
                    var enter = Window.Current.CoreWindow.GetKeyState(VirtualKey.Enter);
                    var shift = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift);
                    if (enter.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down) && shift.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))
                    {                   
                        txtMessage.Text += "\r\n";
                        txtMessage.SelectionStart = txtMessage.Text.Length;
                        e.Handled = true;
                    
                    }
                    else if (enter.HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down))             
                    {                
                    
                        e.Handled = true;                   
                    }
                }
               

    ThanQ

    Ganesh


    Thursday, November 14, 2013 12:32 PM
  • Thanks Ganesh :) Your solution is the only one that worked for me. 
    Monday, April 28, 2014 7:15 PM