[VC++ & XAML] a keydown event bug for virtual touch keyboard input

Category: windows metro apps native

Question

Samuel K. Kim on Thu, 11 Apr 2013 10:56:18


Hi,
I am a beginner for Metro app development and trying to use the keydown event but not the keyup event with a text box element on the XAML. I have created two key event functions (up and down) from the text box, which are the below. The problem is the keydown function works only when the key is up but not down. If I hold the key press down, the event does not come out until the key up, which is the same as the keyup function.

I am working to get keyboard input from the touch softkeyboard. For example, after invoking the touch keyboard with the text box element, I like to play a unique sound only when a key (e.g., 'Q' or 'K') is pressed.

Anyone knows how to fix this problem? I also appreciate that you can help me know the alternative way with an example source code. Thank you a lot in advance.

 
MainPage::OnKeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if((int)e->Key == 112) // F1 key
{
errorText->Text = "F1 Key up";
}

}

MainPage::OnKeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if((int)e->Key == 112) // F1 key
{
errorText->Text = "F1 Key down";
}
}
 
I just found that only space key, backspace and enter key are working for keydown but all other keys are sending the key down event when the key is released on the touch keyboard. So my question must be rephrased to how to make all other keys send the keydown event on the Keydown api? or the other apis?
 
Thank you.

Replies

Amanda Zhu on Fri, 12 Apr 2013 07:22:06


Hello,

I have moved this thread to Building Windows Store apps with C++ forum for better response.

Best regards,

Jesse Jiang on Mon, 15 Apr 2013 02:29:27


Hi,

You should use Window.Current.CoreWindow.KeyDown event instead of the page keydown and keyup event. Maybe there are other controls can catch the keydown event in your page, like button, textbox, etc. Therefore, some key event is missed in your codes.

Please take a look of this thread
http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/56272bc6-6085-426a-8939-f48d71ab12ca/

Best regards,
Jesse

Samuel K. Kim on Tue, 23 Apr 2013 06:53:15


Hi Jesse,

I am now using Window.Current.CoreWindow.KeyDown event instead but the event is still firing when a key pressed. Can you take a look at my simple source code below and give me advice if there is anything missing?  Thank you for your help.

MainPage.xaml.cpp

MainPage::OnCoreKeyDown(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ e)

{ // whenever any key is pressed, play a sound

   LoadClickWaveForXaudio(SimpleAudioWave);

}

MainPage.xaml.h

void OnCoreKeyDown(Windows::UI::CoreWindow^ sender, Windows::UI::Core::KeyEventArgs^ e);

MainPage.xaml

 

<TextBoxName="Text"HorizontalAlignment="Left"TextWrapping="Wrap"Text="Enter Text:"VerticalAlignment="Top"Height="191"Width="1934"Margin="0,120,-568,0"KeyDown="OnCoreKeyDown"KeyUp="OnKeyUp"/>

Samuel K. Kim on Tue, 23 Apr 2013 07:47:51


and I found that Window.Current.CoreWindow.KeyDown cannot be used for C++. Anyone knows how to use CoreWindow class for C++? Thank you.

Sam


Jesse Jiang on Tue, 23 Apr 2013 08:40:22


Hi,

First, we should add AcceleratorKeyActivated event,

		_acceleratorKeyEventToken = Window::Current->CoreWindow->Dispatcher->AcceleratorKeyActivated +=
			ref new TypedEventHandler<CoreDispatcher^, AcceleratorKeyEventArgs^>(this,
			&LayoutAwarePage::CoreDispatcher_AcceleratorKeyActivated);
	

And then, we can get keydown event in that handler.

void LayoutAwarePage::CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher^ sender,
															 AcceleratorKeyEventArgs^ args)
{
	auto virtualKey = args->VirtualKey;

	// Only investigate further when Left, Right, or the dedicated Previous or Next keys
	// are pressed
	if ((args->EventType == CoreAcceleratorKeyEventType::SystemKeyDown ||
		args->EventType == CoreAcceleratorKeyEventType::KeyDown) &&
		(virtualKey == VirtualKey::Left || virtualKey == VirtualKey::Right ||
		(int)virtualKey == 166 || (int)virtualKey == 167))
	{
		auto coreWindow = Window::Current->CoreWindow;
		auto downState = Windows::UI::Core::CoreVirtualKeyStates::Down;
		bool menuKey = (coreWindow->GetKeyState(VirtualKey::Menu) & downState) == downState;
		bool controlKey = (coreWindow->GetKeyState(VirtualKey::Control) & downState) == downState;
		bool shiftKey = (coreWindow->GetKeyState(VirtualKey::Shift) & downState) == downState;
		bool noModifiers = !menuKey && !controlKey && !shiftKey;
		bool onlyAlt = menuKey && !controlKey && !shiftKey;

		if (((int)virtualKey == 166 && noModifiers) ||
			(virtualKey == VirtualKey::Left && onlyAlt))
		{
			// When the previous key or Alt+Left are pressed navigate back
			args->Handled = true;
			GoBack(this, ref new RoutedEventArgs());
		}
		else if (((int)virtualKey == 167 && noModifiers) ||
			(virtualKey == VirtualKey::Right && onlyAlt))
		{
			// When the next key or Alt+Right are pressed navigate forward
			args->Handled = true;
			GoForward(this, ref new RoutedEventArgs());
		}
	}
}

You can take a look LayoutAwarePage.cpp in Grid template.

Best regards,
Jesse

Samuel K. Kim on Tue, 23 Apr 2013 10:11:47


Hi Jesse,

Thank you a lot for your quick reply. Could you give me the link of the Grid template ? I failed to find it from the MSDN forum. Thank you again.

Sam

Samuel K. Kim on Tue, 23 Apr 2013 11:51:12


Hi Jesse,

I found the source code of Grid template. Sorry to interrupt you too much.

I knew that the softkeyboard always sends keyup event for all key press event except Space and Enter keys. When I tested with all keys, only the two keys (Space and Enter) are functioning well in response to key-press. The others are working at releasing key even though using keydown event It seems like the softkeyboard hold the key down event to provide users with choosing a final key character that pops up when the key is kept pressing for one or two seconds. Is this true? If true, is there anyway to correct this? Physical keyboard are working properly for keydown or keyup. This problem happens only with the softkeyboard invoked. Thank you a lot for your help.

Sam