2

How do I get the Key, one had pressed using PreviewKeyDownEvent. Yes there is KeyEventArgs which returns the Key which is pressed. But the problem I am facing is, I am not able to differentiate smaller and lower case characters

I need to check if the person had pressed a or A or anyother characters differentiating with cases

Thanks

Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30

3 Answers3

6

In the PreviewKeyDown event there is not a direct way to get the case of the character entered using what is given in the KeyEventArgs. You could try using the PreviewTextInput event which will give the actual character entered in the TexCompositionEventArgs Text Property.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • -1 There *is* a way of detecting the case of the character entered. – Sheridan Aug 29 '13 at 16:14
  • @Sheridan I said there is not a direct way using the what was given in the eventargs, I did not say there wasn't any way. Your way is not direct. – Mark Hall Aug 29 '13 at 16:19
  • Oh please... if checking the condition `Keyboard.Modifiers == ModifierKeys.Shift` is not direct, then I don't know what is. Either way, I stand by my down vote as you then went on to unnecessarily recommend using a different event altogether... I'm sure you've given many great answers in your time here, but just face it, this particular answer was not a good one. – Sheridan Aug 29 '13 at 16:28
  • @Sheridan If the OP wants to know if the character input is a capital letter using your method, you would first have to check if the caps lock is on, then check if the shift is pressed for both states of the Cap Lock key, then handle the logic to determine what the current key. I just thought it would be much easier to use an event that would give you what was actually pressed. but come what may your opinion is yours. :) – Mark Hall Aug 29 '13 at 16:38
  • 1
    I had not thought of that and I accept your point. As such, I've removed my down vote. – Sheridan Aug 29 '13 at 20:20
1

You need to get the ASCII code for the Key

VB Example Here - many other on Google

Much better example here

Community
  • 1
  • 1
SteveB
  • 1,474
  • 1
  • 13
  • 21
0

Basically you compare the value of KeyEventArgs.Key with whichever key you want:

private void PreviewKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.A) DoSomething();
}

If you want to know whether a capital letter was pressed, you need to check if the SHIFT key was also pressed:

private void PreviewKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Shift) DoSomething();
}

UPDATE >>>

If you need to detect whether the Caps Lock button is pressed as well, you can check for this condition:

if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled) 
{
    if (e.Key == Key.A) DoSomething();
}
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 4
    How do you propose handling it if the caps lock is on? – Mark Hall Aug 29 '13 at 16:09
  • 1
    The reason why I stated what did is that once the CapLock is down it reverses the function of the shift key, plus capital letters are the default when it is down. – Mark Hall Aug 29 '13 at 16:22
  • Might not be a general way to handle this problem. – Sankarann Aug 29 '13 at 17:41
  • That's funny... and I thought we just down voted posts that were way off topic, or provided incorrect information or bad advice. :) Although I accept that my answer may not be the best answer, it still provides a valid way for the question author to solve his problem. – Sheridan Aug 30 '13 at 08:03