Jumat, 08 Oktober 2010

Cek Status Tombol CapsLock, NumLock dan ScrollLock

pada VB.NET untuk melakukan pengecekan apakah tombol CapsLock, Numlock dan ScrollLock dalam posisi On atau Off bisa dilakukan dengan mudah dengan memanfaatkan namespace My yang sudah disediakan.

berikut ini contoh pengecekan tersebut dalam VB.NET:

Module Module1
    Sub Main()
        Console.WriteLine("CapsLock Is ON: {0}", My.Computer.Keyboard.CapsLock)
        Console.WriteLine("NumLock Is ON: {0}", My.Computer.Keyboard.NumLock)
        Console.WriteLine("ScrollLock Is ON: {0}", My.Computer.Keyboard.ScrollLock)
    End Sub
End Module

dalam C#, karena tidak disediakan, kita bisa menggunakan beberapa pendekatan yang berbeda untuk melakukan hal yang sama.

yang berikut ini bisa digunakan untuk console app dan win app dalam c#:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        // an unmanaged function that retrieves the states of each key
        [DllImport("user32.dll",
            CharSet = CharSet.Auto,
            ExactSpelling = true,
            CallingConvention = CallingConvention.Winapi)]
        public static extern short GetKeyState(int keyCode);

        static void Main(string[] args)
        {
            // get the 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;
            // show the status
            Console.WriteLine("CapsLock Is ON: {0}", capsLock);
            Console.WriteLine("NumLock Is ON: {0}", numLock);
            Console.WriteLine("ScrollLock Is ON: {0}", scrollLock);

            // managed code
            bool cekCapsLock = Console.CapsLock;
            bool cekNumLock = Console.NumberLock;
            Console.WriteLine("CapsLock Is ON: {0}", cekCapsLock);
            Console.WriteLine("NumLock Is ON: {0}", cekNumLock);
        }
    }
}

mulai dari .NET Framework versi 2.0, selain menggunakan cara di atas, bisa jg menggunakan class Control.IsKeyLocked seperti yang ditunjukkan pada code berikut ini:

Capslock

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            this.lblCapsLock.Text = "CAPS LOCK: " + Control.IsKeyLocked(Keys.CapsLock);
            this.lblNumLock.Text = "NUM LOCK: " + Control.IsKeyLocked(Keys.NumLock);
            this.lblScrollLock.Text = "SCROLL LOCK: " + Control.IsKeyLocked(Keys.Scroll);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.lblCapsLock.Text = "CAPS LOCK: " + Control.IsKeyLocked(Keys.CapsLock);
            this.lblNumLock.Text = "NUM LOCK: " + Control.IsKeyLocked(Keys.NumLock);
            this.lblScrollLock.Text = "SCROLL LOCK: " + Control.IsKeyLocked(Keys.Scroll);
        }
    }
}

Nb. teknik pengecekan seperti yang dilakukan oleh C# juga bisa diterapkan dengan sedikit penyesuaian penulisan code pada code di VB.NET

Rgds
Adi

Tidak ada komentar: