wpf c# check Enter key and PrtSc pressed

hossein tavakoli 471 Reputation points
2023-10-25T06:31:16.0066667+00:00

Hi dears,

I'm using WPF/C# to create a application for checking keyboard's key work correctly.

I have problems with 3 key:

1- Enter Key (placed inside numeric key) : Enter key I can see IsExtended property in debugging mode but I don't have access it in code.

2- PrtSc key : when I press it windows screenshot app will open and I can't check it pressed.

3- Win key : I can check it but after key press start menu will open, I want to check win key pressed without opening start menu.

my code is here :

private void tabItemKeyboard_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
	string key = (e.Key == Key.System) ? e.SystemKey.ToString() : e.Key.ToString();
	txtKeyPress.Content = key;
	FindLabelKeyPressed(key);
	if (LabelSelected != null)
	{
		LabelSelected.Background = System.Windows.Media.Brushes.Yellow;
	}
	if (key == "F10" || key.ToLower().Contains("alt") || key.ToLower()=="left" || key.ToLower()== "right" || key.ToLower().Contains("home") || key.ToLower().Contains("win"))
		e.Handled = true;
}

please give me a code.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. KOZ6.0 6,735 Reputation points
    2023-10-25T13:34:43.4533333+00:00

    The PrintScreen key can be detected with keyboard hooks. Call the SetWindowsHookEx API to install the WH_MOUSE_LL WH_KEYBOARD_LL hook.

    SetWindowsHookExW

    You can also check whether a key is pressed using the GetKeyState API.

    GetKeyState function (winuser.h)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.