Using C # program to simulate keyboard and mouse

Source: Internet
Author: User
program using System;
Using System.Runtime.InteropServices;
Using System.Text;

Namespace consoleapplication8{
Class class1{
[STAThread]
static void Main (string[] args) {
Display the current status of the keys.
Console.WriteLine (
"**before**\r\ncap: {0}\r\nscr: {1}\r\nnum: {2}",
Keyboard.getstate (virtualkeys.vk_capital)? " On ': Off ',
Keyboard.getstate (virtualkeys.vk_scroll)? " On ': Off ',
Keyboard.getstate (virtualkeys.vk_numlock)? " On ': Off '
);
Toggle all the keys:
Keyboard.setstate (
Virtualkeys.vk_capital,
! Keyboard.getstate (Virtualkeys.vk_capital)
);
Keyboard.setstate (
Virtualkeys.vk_scroll,
! Keyboard.getstate (Virtualkeys.vk_scroll)
);
Keyboard.setstate (
Virtualkeys.vk_numlock,
! Keyboard.getstate (Virtualkeys.vk_numlock)
);
Display new status of the keys.
Console.WriteLine (
"\r\n**after**\r\ncap: {0}\r\nscr: {1}\r\nnum: {2}",
Keyboard.getstate (virtualkeys.vk_capital)? " On ': Off ',
Keyboard.getstate (virtualkeys.vk_scroll)? " On ': Off ',
Keyboard.getstate (virtualkeys.vk_numlock)? " On ': Off '
);
Console.ReadLine ();
}
}
public enum virtualkeys:byte{
Vk_numlock = 0x90,
Vk_scroll = 0x91,
Vk_capital = 0x14
}
Class keyboard{
const UINT KEYEVENTF_EXTENDEDKEY = 0x1;
const UINT KEYEVENTF_KEYUP = 0x2;
[DllImport ("user32.dll")]
static extern short getkeystate (int nvirtkey);
[DllImport ("user32.dll")]
static extern void keybd_event (
Byte BVK,
Byte Bscan,
UINT dwflags,
UINT dwExtraInfo
);
public static bool GetState (Virtualkeys Key) {
Return (getkeystate (int) Key) ==1);
}
public static void SetState (Virtualkeys Key, bool state) {
if (State!=getstate (Key)) {
Keybd_event (
(byte) Key,
0X45,
Keyeventf_extendedkey | 0,
0
);
Keybd_event (
(byte) Key,
0X45,
Keyeventf_extendedkey | Keyeventf_keyup,
0
);
}
}
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.