Click here to Skip to main content
15,436,765 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello
I could not get the key code for those characters which require shift key
like exclamtion mark,ampersand,crate,percent,star,brackets ...etc
in my program when I tried to get the code for ampersand for example ,the first step is to press the shift key the 2nd step is to press the crate key ,but the program immediately return the code for the shift.
which is equal 16 (my keyboard are set on Arabic saudia arabia)
I do not have a chance to press the ampersand

What I have tried:

C#
private void OnKeyDown(object sender, KeyEventArgs e)
{
   MessageBox.Show(e.KeyCode.ToString(), e.KeyValue.ToString());
}
Posted
Updated 12-May-16 13:12pm
v5
Comments
Karthik_Mahalingam 12-May-16 11:29am     CRLF
if (e.Shift) { MessageBox.Show(e.KeyCode.ToString(), e.KeyValue.ToString()); }
Sergey Alexandrovich Kryukov 12-May-16 11:57am     CRLF
It depends on what it is. Please see my comment below. —SA
Sergey Alexandrovich Kryukov 12-May-16 11:56am     CRLF
KeyEventArgs... Which one? Full type name, please. Forms? WPF? And then, the solution is: read MSDN documentation on this type with attention. This problem is not easy; it is very easy. —SA

Personally I would use OnKeyUp

The KeyEventArgs parameter will also give you additional information on which other keys were pressed.

Eg look at this code snippet

C#
private void Form3_KeyUp(object sender, KeyEventArgs e)
{
    Console.WriteLine("{0} : {1}", e.KeyCode, e.KeyValue);
    if(e.Alt) Console.WriteLine("with Alt");
    if (e.Control) Console.WriteLine("with Control");
    if (e.Shift) Console.WriteLine("with Shift");
}

(The output will appear in the output window in Visual Studio)
 
Share this answer
 
Comments
Engineer khalid 12-May-16 17:38pm     CRLF
To Karthik Bangalore i am not looking for keycode for the shift key ,i am looking for crate ,excalamation ,ambersand... To Sergey Alexandrovich my program is a c sharp window form.. i will look at the msdn .my grade will take time
CHill60 12-May-16 19:31pm    
If you want to reply to particular comments then use the "Reply" link next to those comments
Engineer khalid 12-May-16 19:09pm     CRLF
i guess the best thig i have got is to use private void OnKeyPressed(object sender, KeyPressEventArgs e) { switch (e.KeyChar) { case 'q': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case 'w': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case 'e': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case 'r': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case 't': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case 'y': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case '^': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; case '&': MessageBox.Show(e.KeyChar.ToString(), "Hellow"); break; ; }//switch //if(e.KeyChar=='&' && e.KeyValue == 55) ; }
CHill60 12-May-16 19:33pm     CRLF
As I said ... I would use the KeyUp event. Don't use MessageBox in those events. Now have another look at the code you have just posted ... what is the point of a switch if you are doing the same thing for all those cases?
//i guess the best thig i have got is to use
C#
<pre lang="C#">private void OnKeyPressed(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case &#39;q&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;w&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;e&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;r&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;t&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;y&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;^&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
case &#39;&amp;&#39;:
MessageBox.Show(e.KeyChar.ToString(), &quot;Hellow&quot;);
break;
;
}//switch

//if(e.KeyChar==&#39;&amp;&#39; &amp;&amp; e.KeyValue == 55) ;
}</pre>
 
Share this answer
 
Comments
CHill60 12-May-16 19:33pm    
Don't post comments as solutions. If you need to update your post use the Improve question link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900