Guest User

Untitled

a guest
Apr 9th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Mlaker
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  20.         {
  21.             if (e.KeyCode == Keys.Left)
  22.             {
  23.                 Poslji(2);
  24.             }
  25.             else if (e.KeyCode == Keys.Right)
  26.             {
  27.                 Poslji(1);
  28.             }
  29.             else if (e.KeyCode == Keys.Up)
  30.             {
  31.                 Poslji(4);
  32.  
  33.             }
  34.             else if (e.KeyCode == Keys.Down)
  35.             {
  36.                 Poslji(3);
  37.             }
  38.             else if (e.KeyCode == Keys.Space)
  39.             {
  40.                 Poslji(9);
  41.             }
  42.             else if (e.KeyCode == Keys.ShiftKey)
  43.             {
  44.                 Poslji(20);
  45.             }
  46.         }
  47.            
  48.         void Poslji(byte poslji)
  49.         {
  50.             try
  51.             {
  52.                 byte[] b = new byte[1];
  53.                 b[0] = poslji;
  54.                 serialPort1.Write(b, 0, 1);
  55.             }
  56.             catch
  57.             {
  58.                 MessageBox.Show("Napaka pri posiljanju");
  59.             }
  60.         }
  61.  
  62.         private void buttonOdpri_Click(object sender, EventArgs e)
  63.         {
  64.             if (buttonOdpri.Text == "Odpri")
  65.             {
  66.                 serialPort1.PortName = textBoxPort.Text;
  67.                 serialPort1.BaudRate = int.Parse(textBoxHitrost.Text);
  68.                 serialPort1.Open();
  69.                 buttonOdpri.Text = "Zapri";
  70.             }
  71.             else
  72.             {
  73.                 serialPort1.Close();
  74.                 buttonOdpri.Text = "Odpri";
  75.             }
  76.         }
  77.  
  78.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  79.         {
  80.             if (e.KeyCode == Keys.Left)
  81.             {
  82.                 Poslji(6);
  83.             }
  84.             else if (e.KeyCode == Keys.Right)
  85.             {
  86.                 Poslji(5);
  87.             }
  88.             else if (e.KeyCode == Keys.Up)
  89.             {
  90.                 Poslji(8);
  91.             }
  92.             else if (e.KeyCode == Keys.Down)
  93.             {
  94.                 Poslji(7);
  95.             }
  96.             else if (e.KeyCode == Keys.Space)
  97.             {
  98.                 Poslji(10);
  99.             }
  100.             else if (e.KeyCode == Keys.ShiftKey)
  101.             {
  102.                 Poslji(21);
  103.             }
  104.         }
  105.  
  106.        
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment