如何在c#.net中捕获大写字母和数字键 [英] How to Capture Caps And Num Key in c#.net

查看:110
本文介绍了如何在c#.net中捕获大写字母和数字键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试以MDI形式捕获键盘的Num lock和Caps Lock键以获取用户信息

因此,有一种方法可以识别开/关按键.

大写和数字锁定状态也会根据按下的键"和"IF"而更改,因为在MDI表单中放置了向上"和向下"键,这可能会增加项目的负担.

问候....
Amit Tank

Hey All,

I am trying to capture Num lock and Caps Lock keys of keyboard for User Information in MDI Form

So There is a way to identify keys on/off.

This Caps and Num lock Status are also changed as per the Key Pressed and IF there Short way because Key Up and Down keys are put in the MDI Form and it may increase a load of project.


Regards....
Amit Tank

推荐答案

尝试:
using System.Runtime.InteropServices;
...
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
    ...

    // Get they key state and store it as bool
    bool CapsLock = (((ushort) GetKeyState(0x14)) & 0xffff) != 0;
    bool NumLock = (((ushort) GetKeyState(0x90)) & 0xffff) != 0;
    bool ScrollLock = (((ushort) GetKeyState(0x91)) & 0xffff) != 0;


您在这里:

http://msdn.microsoft.com/en-us/library/ms646314 [ ^ ]

代码在这里:

Here you go:

http://msdn.microsoft.com/en-us/library/ms646314[^]

Code here:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class CapsLockControl
{
    [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,UIntPtr dwExtraInfo);
    const int KEYEVENTF_EXTENDEDKEY = 0x1;
    const int KEYEVENTF_KEYUP = 0x2;

    public static void Main()
    {
        if (Control.IsKeyLocked(Keys.CapsLock))
        {
            Console.WriteLine("Caps Lock key is ON.  We'll turn it off");
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
            keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                (UIntPtr) 0);
        }
        else
        {
            Console.WriteLine("Caps Lock key is OFF");
        }
    }
}


这篇关于如何在c#.net中捕获大写字母和数字键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆