topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday April 16, 2024, 11:22 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Distinguish Enter and Numpad Enter Key c#.net  (Read 11562 times)

stophereareyou

  • Participant
  • Joined in 2010
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
Distinguish Enter and Numpad Enter Key c#.net
« on: March 16, 2013, 04:24 AM »
Hello Every one,

Is there any way to distinguish Numpad Enter key and Normal Enter Key,

in c#.net<code> KeyCode, KeyCode</code> and <code>KeyValue</code> for both keys are same,

is there any other way or some workaround for distinguishing Events raised by both keys?!


Firdaus Shaikh
('',)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Distinguish Enter and Numpad Enter Key c#.net
« Reply #1 on: March 16, 2013, 04:37 PM »
Though the Keys enum has identical values for Enter and Return (it also has a number of other duplicates), ToString shows a different value.

If you run this linq expression in linqpad, you'll see what I mean.

Code: C# [Select]
  1. Enum.GetNames(typeof(System.Windows.Forms.Keys))
  2.     .GroupBy(k => Enum.Parse(typeof(System.Windows.Forms.Keys), k))
  3.     .Where(g => g.Count() > 1)
  4.     .Select(g => String.Join(", ", g.Select(k => k.ToString()).ToArray())).Dump();

Check for the tostring if you really need to know the difference.

stophereareyou

  • Participant
  • Joined in 2010
  • *
  • default avatar
  • Posts: 14
    • View Profile
    • Donate to Member
Re: Distinguish Enter and Numpad Enter Key c#.net
« Reply #2 on: March 17, 2013, 02:11 PM »
Though the Keys enum has identical values for Enter and Return (it also has a number of other duplicates), ToString shows a different value.

If you run this linq expression in linqpad, you'll see what I mean.

Code: C# [Select]
  1. Enum.GetNames(typeof(System.Windows.Forms.Keys))
  2.     .GroupBy(k => Enum.Parse(typeof(System.Windows.Forms.Keys), k))
  3.     .Where(g => g.Count() > 1)
  4.     .Select(g => String.Join(", ", g.Select(k => k.ToString()).ToArray())).Dump();

Check for the tostring if you really need to know the difference.

Hello wraith808,
I did not understand all of it but,
Tried it in C# as follows

           
 foreach (var VARIABLE in Enum.GetNames(typeof (System.Windows.Forms.Keys))
                .GroupBy(k => Enum.Parse(typeof (System.Windows.Forms.Keys), k))
                .Where(g => g.Count() > 1)
                .Select(g => String.Join(", ", g.Select(k => k.ToString()).ToArray())))
            {
                richTextBox1.AppendText(VARIABLE+"\r\n");
            }

Got following...
Enter, Return
CapsLock, Capital
HangulMode, HanguelMode, KanaMode
KanjiMode, HanjaMode
IMEAccept, IMEAceept
Prior, PageUp
PageDown, Next
Snapshot, PrintScreen
OemSemicolon, Oem1
Oem2, OemQuestion
Oem3, Oemtilde
Oem4, OemOpenBrackets
OemPipe, Oem5
OemCloseBrackets, Oem6
OemQuotes, Oem7
Oem102, OemBackslash

that is Enter and Return does have same value,
Thanks for your time and code, I might have missed your point if you are telling something more than I've understood,

but is there any difference between Main Enter Key and Enter Key on Numpad!?

Firdaus Shaikh
('',)

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Distinguish Enter and Numpad Enter Key c#.net
« Reply #3 on: March 17, 2013, 02:32 PM »
Enter and Return don't have the same value if you check the tostring.  

for example:

Code: C# [Select]
  1. switch (keyVariable.ToString())
  2. {
  3.   case "Enter":
  4.     // they pressed the numpad enter key
  5.     Console.WriteLine("Numpad Enter Key");
  6.     break;
  7.   case "Return":
  8.     // they pressed the keyboard enter key
  9.     Console.WriteLine("Keyboard Enter Key");
  10.     break;
  11. }

Don't take my word on which is which... I didn't check.  But the strings are different, so you can check for them is the point.