如何制作它以便我不必重新点击应用程序以使keydown功能起作用?

时间:2016-02-01 23:48:39

标签: c#

有些事情一直困扰着我,而且事实上我必须点击并点击应用程序中的按键功能才能工作。有什么办法可以解决吗? 代码以防万一你需要它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dog_simulator
{

public partial class Form1 : Form
{
    public bool KeyPreview { get; set; }
    bool right;
    bool left;
    bool up;
    bool down;
    public int left_var = 5;
    public int right_var = 5;
    public Form1()
    {
        this.KeyPreview = true;
        InitializeComponent();
        Pee.Left += 220;



    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    public void button2_Click(object sender, EventArgs e)
    {
        panel1.Visible = false;
    }

    public void button1_Click(object sender, EventArgs e)
    {
        panel1.Visible = false;
    }

    public void Form1_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode == Keys.Left)
        {
            left = true;
            Console.WriteLine("Left has been pressed");
        }
        if(e.KeyCode == Keys.Right)
        { 
            right = true;
            Console.WriteLine("Right has been pressed");
        }
        if(e.KeyCode == Keys.Up)
        {
            up = true;
            Console.WriteLine("Up has been pressed");
        }
        if(e.KeyCode == Keys.Down)
        {
            down = true;
            Console.WriteLine("Down has been pressed");
        }
    }

    public void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
            left = false;
            Console.WriteLine("Left has been let go of");
        }
        if (e.KeyCode == Keys.Right)
        {
            right = false;
            Console.WriteLine("Right has been let go of");
        }
        if (e.KeyCode == Keys.Up)
        {
            up = false;
            Console.WriteLine("Up has been let go of");
        }
        if (e.KeyCode == Keys.Down)
        {
            down = false;
            Console.WriteLine("Down has been let go of");
        }


    }

    public void timer1_Tick(object sender, EventArgs e)
    {
            if (right == true)
            {
                player.Left += left_var;

            }
            if (left == true)
            {
                player.Left -= left_var;
            }
            Invalidate();
            if (up == true)
            {
                player.Top -= left_var;
            }
            if (down == true)
            {
                player.Top += left_var;
            }
    }

        private void dog_Click(object sender, EventArgs e)
        {

        }

        private void player_Click(object sender, EventArgs e)
        {

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

    private void PeeTime_Tick(object sender, EventArgs e)
    {

        if(panel1.Visible == false)
        {
            Pee.Left -= 5;
            if (Pee.Bounds.IntersectsWith(pee2.Bounds))
            {
                Pee.Left += 220;
            }
        }


    }
}
}

0 个答案:

没有答案