C# keyboard event handler rectangle -


i new c# , try make keyboard event. should display when keys w, a, s or d pressed. first plan show picturebox , change picture if right keys pressed.

but searched internet found similar in java http://docs.oracle.com/javase/8/javafx/sample-apps/keyboardexample.zip , looks this: enter image description here

as can understand code drawing rectangle letter in it. looked msdn , found example drawing rectangle: https://msdn.microsoft.com/de-de/library/sx8yykw8(v=vs.110).aspx

unfortunately stucking in drawing. use toolbox add things form. double-click , write code inside braces. there no "rectangle" in toolbox, not sure how add it.

this code far:

public partial class form1 : form {     public form1()     {         initializecomponent();         //stay on top         this.topmost = true;         //does not work. removes border can't move window after         this.formborderstyle = system.windows.forms.formborderstyle.none;     }      private void form1_load(object sender, eventargs e)     {         //can delete this?                 }      private void form1_keypress(object sender, keypresseventargs e)     {         if (e.keychar >= 65 && e.keychar <= 122)         {             switch (e.keychar)             {                 //if pressed w or w                 case (char)119:                 case (char)87:                     console.writeline(e.keychar);                     break;                 //if pressed or                 case (char)97:                 case (char)65:                     console.writeline(e.keychar);                     break;                 //if pressed s or s                 case (char)83:                 case (char)115:                     console.writeline(e.keychar);                     break;                 //if pressed d or d                 case (char)100:                 case (char)68:                     console.writeline(e.keychar);                     break;                 //other keys                 default:                     lblmessage.text = "key not supported";                     //does not work                     //timer1_tick();                     break;             }         }     }      private void timer1_tick(object sender, eventargs e)     {         lblmessage.hide();     } } 

and here how form looks now:

enter image description here

other things stucking @ moment:

  • how can call timer form1_keypress hide lblmessage after seconds?

  • remove border without losing ability move window (like this.formborderstyle = system.windows.forms.formborderstyle.none; example)

edit: changed code newest working state.

welcome world of windows desktop programming!

you have 2 choices here; can either:

  1. add components wasd form using design view (and since have w, a, s, d boxes there looks have added them in) , in form1_keypress() handler, update properties of boxes. can simple following, make sure change correct component name:

    //if pressed w or w case (char)119: case (char)87:         console.writeline(e.keychar);         button1.backcolor = color.red;//highlight w         button2.backcolor = color.empty;//ignore         button3.backcolor = color.empty;//ignore s         button3.backcolor = color.empty;//ignore d         break; 
  2. override form's ondraw() handler , paint boxes directly on screen. harder gives lot more power.

turning off label easy. in form1_load() handler make sure set timer1's timeout property:

private void form1_load(object sender, eventargs e) {     timer1.interval = 5000;//in ms = thousandths-of-a-second } 

turn timer on in form1_keypress() handler:

private void form1_keypress(object sender, keypresseventargs e) {     ...     lblmessage.enabled = true;     timer1.start(); } 

do work , turn off timer in timer1_tick() handler:

private void timer1_tick(object sender, eventargs e) {     lblmessage.enabled = false;     timer1.stop(); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -