c# 判断 按键 状态

来源:互联网 发布:cf透视辅助源码 编辑:程序博客网 时间:2024/05/16 00:33

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern short GetAsyncKeyState(int nVirtKey);
        const int VK_CONTROL = 0x11;
        const int VK_ALT = 0x12;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
        }
        //private void Form1_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.Modifiers.CompareTo(Keys.Control) == 0) ctrlDowned = true;
        //    timer1.Enabled = true;
        //}
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetAsyncKeyState(VK_CONTROL).ToString());
            MessageBox.Show(GetAsyncKeyState(VK_ALT).ToString());
            ////if (GetAsyncKeyState(VK_CONTROL & 0x8000) != 0)
            if (GetAsyncKeyState(VK_CONTROL) < 0)
            {
                MessageBox.Show("ctrl key down");
            }
            else
            {
                MessageBox.Show("ctrl key up");
            }
        }
        //private void timer1_Tick(object sender, EventArgs e)
        //{
        //    timer1.Enabled = false;
        //    ctrlDowned = false;
        //}
    }
}

原创粉丝点击