Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine whether modifier key was pressed

Tags:

c#

key

modifier

I know how to obtain which modifier key was pressed in C# but I don't know how I can actually check if any modifier key was pressed. I need to check it in KeyUp event, is it possible any other way than doing something like if(e.KeyCode != Keys.Control && e.KeyCode != Keys.Alt && ...) ? Thanks.

like image 676
haluzak Avatar asked Dec 07 '22 22:12

haluzak


2 Answers

if ((Control.ModifierKeys & Keys.Shift) != 0) 

will help you detect whether a modifier key (e.g. ctrl or shift) was pressed. Check the Post below for reference:

How to detect the currently pressed key?

like image 74
reggie Avatar answered Dec 23 '22 18:12

reggie


To make sure no modifier key is pressed you can check if ModifierKey equals Keys.None.

if (ModifierKeys == Keys.None) ...
like image 23
caycothu Avatar answered Dec 23 '22 18:12

caycothu