locked
Key Press Eventhandler

    Question

  • I try to start a certain function If the space bar is pressed.
    I also found a tutorial for it but it is only partly working:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh868246.aspx
    (Ok for some reason I cant post this as link :/ )

    It looks like that I am missing an event Handler bc the function is working when I am in a text box or just pressed an other control. But when I pressed in an empty space from my app so no control is active the event is not triggered.

    This is my code: The same as in the tutorial

    But I noticed if I only put the Grid_KeyDown funktion in it does exectly the same think. So the ref new ... Programmatic does nothing.

    void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
    {
    	(void) e;	// Unused parameter
    	this->Loaded+=ref new RoutedEventHandler(this,&MainPage::ProgrammaticFocus);
    }
    
    void MainPage::ProgrammaticFocus(Object^ sender, RoutedEventArgs^ e) {
        this->Focus(Windows::UI::Xaml::FocusState::Programmatic);
    }
    
    void MainPage::Grid_KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
    {
    	if (e->Key == Windows::System::VirtualKey::Space) playPausefunc();
    }

    For my  OnWindowSizeChanged  Event I have this line in the constructor: Is there maybe something similar that I can use for the KeyPress event?

        Window::Current->CoreWindow->SizeChanged += ref new TypedEventHandler<Windows::UI::Core::CoreWindow^, Windows::UI::Core::WindowSizeChangedEventArgs^>(this, &MainPage::OnWindowSizeChanged);

    Saturday, June 1, 2013 9:24 PM

Answers

  • Try something like this:

    cpp:

    #include "pch.h"
    #include "MainPage.xaml.h"
    
    using namespace HelpWithKeyDown;
    
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Navigation;
    using namespace Windows::UI::Core;
    using namespace Windows::UI::Input;
    
    MainPage::MainPage()
    {
    	InitializeComponent();		
    }
    
    void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
    {
    	(void) e;	// Unused parameter
    
    	Window::Current->CoreWindow->KeyDown+=ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &MainPage::OnKeyDown);
    }
    
    
    void MainPage::OnKeyDown(CoreWindow^ sender, KeyEventArgs^ args)
    {
        auto Key = args->VirtualKey;
    
       if (Key == Windows::System::VirtualKey::Space)
    	{
    		Status->Text = "Key was Space";
    	}
    	else
    	{
    		Status->Text = "Key was Other";
    	}
    }

    and .h

    #pragma once
    
    #include "MainPage.g.h"
    
    namespace HelpWithKeyDown
    {
    	public ref class MainPage sealed
    	{
    	public:
    		MainPage();
    
    	protected:
    		virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
    		void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);		 
    	};
    }

    • Proposed as answer by jrboddie Sunday, June 2, 2013 1:55 AM
    • Marked as answer by Pascal90909 Sunday, June 2, 2013 3:02 AM
    Sunday, June 2, 2013 1:52 AM

All replies

  • Try something like this:

    cpp:

    #include "pch.h"
    #include "MainPage.xaml.h"
    
    using namespace HelpWithKeyDown;
    
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Navigation;
    using namespace Windows::UI::Core;
    using namespace Windows::UI::Input;
    
    MainPage::MainPage()
    {
    	InitializeComponent();		
    }
    
    void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
    {
    	(void) e;	// Unused parameter
    
    	Window::Current->CoreWindow->KeyDown+=ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &MainPage::OnKeyDown);
    }
    
    
    void MainPage::OnKeyDown(CoreWindow^ sender, KeyEventArgs^ args)
    {
        auto Key = args->VirtualKey;
    
       if (Key == Windows::System::VirtualKey::Space)
    	{
    		Status->Text = "Key was Space";
    	}
    	else
    	{
    		Status->Text = "Key was Other";
    	}
    }

    and .h

    #pragma once
    
    #include "MainPage.g.h"
    
    namespace HelpWithKeyDown
    {
    	public ref class MainPage sealed
    	{
    	public:
    		MainPage();
    
    	protected:
    		virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
    		void OnKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ args);		 
    	};
    }

    • Proposed as answer by jrboddie Sunday, June 2, 2013 1:55 AM
    • Marked as answer by Pascal90909 Sunday, June 2, 2013 3:02 AM
    Sunday, June 2, 2013 1:52 AM
  • Thx it is partly working. Whit the space bar I want to pause and resume an endless loop so its kind of a pause button. The problem with this solution seems like it is triggering the event endlessly. What I mean is its starts and pause the loop as long as you press the button. But I only want to trigger the function one time for each press. What would be the easiest solution for that? 
    Sunday, June 2, 2013 2:11 AM
  • Use a boolean variable that is set the first time a space is detected in the handler?
    You might also look at the KeyUp event to help here.
    • Proposed as answer by jrboddie Sunday, June 2, 2013 3:01 AM
    • Unproposed as answer by jrboddie Sunday, June 2, 2013 3:02 AM
    • Edited by jrboddie Sunday, June 2, 2013 3:03 AM
    Sunday, June 2, 2013 2:38 AM
  • The next time I press the spacebar the program should resume. So If I would use a boolean it would work only one time and If I reset the boolen it gets triggered again right away.

    With the solution from the tutorial the event was only triggered once. 

    But the really odd think is, I only get this phenomena with my space bar. With other keys its only triggered again when I really press the key for a while. Maybe its something with my Logitech keyboard. 

    So thank you very much for your help. I either gone change something on my code now to check if the event was already triggered or decide on a different key ;)

    EDIT: I think I know why it was behaving this way. I have a button that does the same think as the space bar. Every time I clicked the button it was the last active one. Windows automatically assigns the space bar as click action for the last button that was used. So the same event was triggered twice :/ 


    Sunday, June 2, 2013 3:02 AM
  • Try detecting KeyUp also. Use KeyUp to reset the Boolean.
    • Edited by jrboddie Sunday, June 2, 2013 3:26 AM
    Sunday, June 2, 2013 3:25 AM
  • This is working, I set the boolean as condition for the button that is doing the same think as the space bar. So the Button now is only triggering the function when the space bar is not pressed. 

    Thx again ;)

    Sunday, June 2, 2013 3:33 AM