Klavye ile Nesne Kontrol (Visual Basic-Studio 2008-2010)

Aerrow

Üye
Katılım
8 Eki 2012
Mesajlar
94
Puanları
1
Yaş
32
Konum
NETAŞ
Dim yon As String
Dim surat As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then yon = "asagi"
If KeyCode = vbKeyUp Then yon = "yukari"
If KeyCode = vbKeyLeft Then yon = "sol"
If KeyCode = vbKeyRight Then yon = "sag"
End Sub
Private Sub Form_Load()
surat = 200
End Sub

Private Sub Timer1_Timer()
Select Case yon
Case "sol":
If nesne.Left <= (-nesne.Width) Then
nesne.Left = Form1.ScaleWidth
Else
nesne.Left = nesne.Left - surat
End If
Case "sag":
If nesne.Left >= Form1.ScaleWidth Then
nesne.Left = (-nesne.Width)
Else
nesne.Left = nesne.Left + surat
End If
Case "yukari":
If nesne.top <= (-nesne.Width) Then
nesne.top = Form1.ScaleHeight
Else
nesne.top = nesne.top - surat
End If
Case "asagi":
If nesne.top >= Form1.ScaleHeight Then
nesne.top = (-nesne.Width)
Else
nesne.top = nesne.top + surat
End If
End Select
End Sub

Yukarıdaki kodu ne yaptıysam ne ettiysem çalıştıramadım gerekli form nesnelerini de ekledim ama bir türlü olmuyor.Kodu çalıştırıp projeyi upload edip link atabilen olursa çok sevinirim.Çok acil lazım.

Şimdiden Teşekkürler.
 
VB de uzman olmasam da case içerisinde string kontrolünü bu şekilde yapman yanlış.

Case 1:
Case 2:
Case 3:

şekli daha iyi olur diye düşünüyorum.

Bunun dışında event operasyonlarını timer içerisinde çok fazla zaman almayacak şekilde yapmanı tavsiye ederim.
Ki zaten formların kontrolünü de keyboard event içerisinde yapabilirsin çünkü tuş basılınca işlem yapılıyor.

Diğer konu ise kullanıcı sadece tek tuşa basmaz diğer tuşların da kombinasyonunu düşünmen gerekli.
 
Merhaba,

Aşağıda verdiğim kodlar ile bu işe yapabilirsin!

Projede 2 form olacak. Bunlardan birincisinde Comport ayarları yapılacak, ikincisinde ise tuşları gösteren işaretler bulunacak. Tuşlar bu ikinci form açıkken kullanılabilecek. Yani önce form1 açılır, sonrada kullanacağımız comport seçilir ensonda başla butonu ile 2.form açılır. Buradanda yön tuşlarına klavyeden basarak, seriporttan ilgili tuş için yazdığımız bilgi gönderilir.

Not : Ben PictureBoxlara image olarak okları ekledim senden benzer birşey yaparsan görsellik artar.

1. formdaki nesneler;

Label2
Combobox1
Button1
Button2

2.formdaki nesneler;

Picturebox1
Picturebox2
Picturebox3
Picturebox4
Label1
Serialport1

Kodlara gelince;

Form1 Public Class Form1
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Form2.Label1.Text =
"------"
Button1.Enabled = True
Button2.Enabled = False
ComboBox1.Enabled = True
EndSub
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = ""Then
MsgBox("ComPort Kutusu Boş Olamaz! Bir ComPort Seçiniz", MsgBoxStyle.Exclamation, "Uyarı")
ComboBox1.Focus()
Else
Form2.SerialPort1.PortName = ComboBox1.Text
Form2.SerialPort1.Open()
ComboBox1.Enabled =
False
Button2.Enabled = True
Button1.Enabled = False
Form2.Show()
Form2.Select()
EndIf
EndSub
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.SerialPort1.Close()
ComboBox1.Enabled =
True
Button1.Enabled =True
Button2.Enabled = False
EndSub
End
Class

Form2
Imports System.IO
Imports System.Windows.Forms
PublicClass Form2

PrivateSub Form2_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMe.KeyDown
If SerialPort1.IsOpen = FalseThen
MsgBox("ComPort Kapalı! Başla Düğmesi ile ComPortu Açmalısınız?", MsgBoxStyle.Exclamation, "Uyarı")
ExitSub
Else
If e.KeyCode = Keys.Up Then
PictureBox1.Visible = True
PictureBox2.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
Label1.Text = " UP "
SerialPort1.Write(Label1.Text)
EndIf
If e.KeyCode = Keys.Right Then
PictureBox2.Visible = True
PictureBox1.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
Label1.Text = " RIGHT > "
SerialPort1.Write(Label1.Text)
EndIf
If e.KeyCode = Keys.Down Then
PictureBox3.Visible = True
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox4.Visible = False
Label1.Text = " DOWN "
SerialPort1.Write(Label1.Text)
EndIf
If e.KeyCode = Keys.Left Then
PictureBox4.Visible = True
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox3.Visible = False
Label1.Text = " < LEFT "
SerialPort1.Write(Label1.Text)
EndIf
EndIf
EndSub
End
Class
 
Sizin anlattığınız yöntemi ben biliyorum zaten de benim istediğim daha farklı bir şey . Arabanın kilometre göstergesindeki ibrenin ben klavyeye basılı tuttuğum süre boyunca belirli bir hızda artması yani soldan sağa doğru hareket etmesi elimi çektiğimde de yavaşça tekrar sola doğru gelmesi.
 
Şöyle Projeyi lazarus ile gerçekleştirdim:

Aşağıdakine benzer bir form oluştur formun özelliklerinde KeyPreview := True olmalı
Bir timer nesnesi ekle nesnenin interval özelliği 50ms civarında sana bağlı isteğe göre değişebilir...

gzls3.jpg



Daha sonra şu kodları oluşturman gerekli:

Kod:
unit main;


{$mode objfpc}{$H+}


interface


uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls


  ,Windows;


type


  { TForm1 }


  TForm1 = class(TForm)
    pnl_Speed: TPanel;
    shp_Meter: TShape;
    Timer1: TTimer;
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;


const
    speed_inc = 4;
    speed_dwn = 3;
    tim = 1/24/60/60;
var
  Form1: TForm1;


  be_wait : tdatetime;


implementation


{$R *.lfm}


{ TForm1 }


procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  //
  Case Key Of
    VK_UP   : begin
                if shp_Meter.Left<pnl_Speed.Width-shp_Meter.Width then
                  shp_Meter.Left := shp_Meter.Left + speed_inc;


                be_wait := now + (tim*1);  //  1 second
              end;
    VK_DOWN : begin
                if shp_Meter.Left>0 then
                  shp_Meter.Left := shp_Meter.Left - speed_inc;
              end;
  End;


end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin


  if now>be_wait then
    if shp_Meter.Left<=0 then
       shp_Meter.Left := 0
       else
          shp_Meter.Left := shp_Meter.Left - speed_dwn;


  if shp_Meter.Left>=pnl_Speed.Width then
     shp_Meter.Left := pnl_Speed.Width-shp_Meter.Width;


end;


end.

Tabi gerçek fizik kuralları gereği eğim vs.. yok düz mantık ileri geri giden bir sistem.

İyi çalışmalar
 

Forum istatistikleri

Konular
127,950
Mesajlar
913,831
Kullanıcılar
449,595
Son üye
Ferden1011

Yeni konular

Geri
Üst