Как зарегистрировать кнопку клавиатуры на какое-нибудь действие?
Код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.NumPad1)
{
//Thread.Sleep(1500);
label1.Text = "Привет";
}
if (e.KeyCode == Keys.NumPad2)
{
Thread.Sleep(2500);
label1.Text = "Пока";
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.NumPad1)
{
//Thread.Sleep(1500);
label1.Text = "Привет";
}
if (e.KeyCode == Keys.NumPad2)
{
Thread.Sleep(2500);
label1.Text = "Пока";
}
}
}
}
Если нужна клавиатурная комбинация, то я бы предложил RegisterHotKey, но у тебя острый, и в нём наверняка есть своё решение, управляемое.
Функционал программы должен быть таковым! Ригистрируется кнопка 1 к примеру! При запущенной программы, при нажатии на кнопку 1, должны происходить следующие действия! Нажатие клавиши Enter, ввод цифр и опять нажатие кнопки Enter, и всё это должно происходить при нажатие на одну кнопку.
А это зачем???? Если вызов проги то в свойствах ярлыка можно назначить клавиши
Цитата: VadikVB
Функционал программы должен быть таковым! Ригистрируется кнопка 1 к примеру! При запущенной программы, при нажатии на кнопку 1, должны происходить следующие действия! Нажатие клавиши Enter, ввод цифр и опять нажатие кнопки Enter, и всё это должно происходить при нажатие на одну кнопку.
Правильно ли я понимаю, что комбинация клавиш актуальна только для этой программы в любом состоянии, даже в неактивном окне?
То есть, к примеру, висят у меня резидентом где-то в трее две программулины, которые реагируют на нажатие, допустим PrintScreen. Но отработает одна, потому что у одной реакция на keydown, а у другой на keyup.
Жду ответы, спасибо что помогаете :)
P.S
Пол дела сделано! Продолжение... теперь надо сделать вместо того что бы выводилось сообщение MessageBox.Show, нужно чтоб при нажатии на NumPad1 нажимался Enter, некоторые цифры (которые я введу в ручную перед автоматизацией в самой программе) и опять Enter.
Hooks.cs
Код:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Hooks
{
public static class KBDHook
{
#region Declarations
public delegate void HookKeyPress ( LLKHEventArgs e );
public static event HookKeyPress KeyUp;
public static event HookKeyPress KeyDown;
[StructLayout(LayoutKind.Sequential)]
struct KBDLLHOOKSTRUCT
{
public uint vkCode;
public uint scanCode;
public KBDLLHOOKSTRUCTFlags flags;
public uint time;
public IntPtr dwExtraInfo;
}
[Flags]
enum KBDLLHOOKSTRUCTFlags : int
{
LLKHF_EXTENDED = 0x01,
LLKHF_INJECTED = 0x10,
LLKHF_ALTDOWN = 0x20,
LLKHF_UP = 0x80,
}
static IntPtr hHook = IntPtr.Zero;
static IntPtr hModule = IntPtr.Zero;
static bool hookInstall = false;
static bool localHook = true;
static API.HookProc hookDel;
#endregion
/// <summary>
/// Hook install method.
/// </summary>
public static void InstallHook()
{
if (IsHookInstalled) return;
hModule = Marshal.GetHINSTANCE(AppDomain.CurrentDomain.GetAssemblies()[0].GetModules()[0]);
hookDel = new API.HookProc(HookProcFunction);
if (localHook)
hHook = API.SetWindowsHookEx(API.HookType.WH_KEYBOARD,
hookDel, IntPtr.Zero, AppDomain.GetCurrentThreadId());
else
hHook = API.SetWindowsHookEx(API.HookType.WH_KEYBOARD_LL,
hookDel, hModule, 0);
if (hHook != IntPtr.Zero)
hookInstall = true;
else
throw new Win32Exception("Can't install low level keyboard hook!");
}
/// <summary>
/// If hook installed return true, either false.
/// </summary>
public static bool IsHookInstalled
{
get { return hookInstall && hHook != IntPtr.Zero; }
}
/// <summary>
/// Module handle in which hook was installed.
/// </summary>
public static IntPtr ModuleHandle
{
get { return hModule; }
}
/// <summary>
/// If true local hook will installed, either global.
/// </summary>
public static bool LocalHook
{
get { return localHook; }
set
{
if (value != localHook)
{
if (IsHookInstalled)
throw new Win32Exception("Can't change type of hook than it install!");
localHook = value;
}
}
}
/// <summary>
/// Uninstall hook method.
/// </summary>
public static void UnInstallHook()
{
if (IsHookInstalled)
{
if (!API.UnhookWindowsHookEx(hHook))
throw new Win32Exception("Can't uninstall low level keyboard hook!");
hHook = IntPtr.Zero;
hModule = IntPtr.Zero;
hookInstall = false;
}
}
/// <summary>
/// Hook process messages.
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
static IntPtr HookProcFunction ( int nCode, IntPtr wParam, [In] IntPtr lParam ) {
if (nCode == 0) {
LLKHEventArgs args = null;
if (localHook) {
bool pressed = false;
if (lParam.ToInt32() >> 31 == 0)
pressed = true;
Keys keys = (Keys)wParam.ToInt32();
args = new LLKHEventArgs(keys, pressed, 0U, 0U);
if (pressed) {
if (KeyUp != null)
KeyUp(args);
} else {
if (KeyDown != null)
KeyDown(args);
}
} else {
KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
bool pressed = false;
if (wParam.ToInt32() == 0x100 || wParam.ToInt32() == 0x104)
pressed = true;
Keys keys = (Keys)kbd.vkCode;
args = new LLKHEventArgs(keys, pressed, kbd.time, kbd.scanCode);
if (pressed) {
if (KeyUp != null)
KeyUp(args);
} else {
if (KeyDown != null)
KeyDown(args);
}
}
if (args != null && args.Hooked)
return (IntPtr)1;
}
return API.CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
public class LLKHEventArgs
{
Keys keys;
bool pressed;
uint time;
uint scCode;
public LLKHEventArgs(Keys keys, bool pressed, uint time, uint scanCode)
{
this.keys = keys;
this.pressed = pressed;
this.time = time;
this.scCode = scanCode;
}
/// <summary>
/// Key.
/// </summary>
public Keys Keys
{ get { return keys; } }
/// <summary>
/// Is key pressed or no.
/// </summary>
public bool IsPressed
{ get { return pressed; } }
/// <summary>
/// The time stamp for this message, equivalent to what GetMessageTime would return for this message.
/// </summary>
public uint Time
{ get { return time; } }
/// <summary>
/// A hardware scan code for the key.
/// </summary>
public uint ScanCode
{ get { return scCode; } }
/// <summary>
/// Is user hook key.
/// </summary>
public bool Hooked { get; set; }
}
static class API
{
public delegate IntPtr HookProc(int nCode, IntPtr wParam, [In] IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, [In] IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn,
IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
}
}
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Hooks
{
public static class KBDHook
{
#region Declarations
public delegate void HookKeyPress ( LLKHEventArgs e );
public static event HookKeyPress KeyUp;
public static event HookKeyPress KeyDown;
[StructLayout(LayoutKind.Sequential)]
struct KBDLLHOOKSTRUCT
{
public uint vkCode;
public uint scanCode;
public KBDLLHOOKSTRUCTFlags flags;
public uint time;
public IntPtr dwExtraInfo;
}
[Flags]
enum KBDLLHOOKSTRUCTFlags : int
{
LLKHF_EXTENDED = 0x01,
LLKHF_INJECTED = 0x10,
LLKHF_ALTDOWN = 0x20,
LLKHF_UP = 0x80,
}
static IntPtr hHook = IntPtr.Zero;
static IntPtr hModule = IntPtr.Zero;
static bool hookInstall = false;
static bool localHook = true;
static API.HookProc hookDel;
#endregion
/// <summary>
/// Hook install method.
/// </summary>
public static void InstallHook()
{
if (IsHookInstalled) return;
hModule = Marshal.GetHINSTANCE(AppDomain.CurrentDomain.GetAssemblies()[0].GetModules()[0]);
hookDel = new API.HookProc(HookProcFunction);
if (localHook)
hHook = API.SetWindowsHookEx(API.HookType.WH_KEYBOARD,
hookDel, IntPtr.Zero, AppDomain.GetCurrentThreadId());
else
hHook = API.SetWindowsHookEx(API.HookType.WH_KEYBOARD_LL,
hookDel, hModule, 0);
if (hHook != IntPtr.Zero)
hookInstall = true;
else
throw new Win32Exception("Can't install low level keyboard hook!");
}
/// <summary>
/// If hook installed return true, either false.
/// </summary>
public static bool IsHookInstalled
{
get { return hookInstall && hHook != IntPtr.Zero; }
}
/// <summary>
/// Module handle in which hook was installed.
/// </summary>
public static IntPtr ModuleHandle
{
get { return hModule; }
}
/// <summary>
/// If true local hook will installed, either global.
/// </summary>
public static bool LocalHook
{
get { return localHook; }
set
{
if (value != localHook)
{
if (IsHookInstalled)
throw new Win32Exception("Can't change type of hook than it install!");
localHook = value;
}
}
}
/// <summary>
/// Uninstall hook method.
/// </summary>
public static void UnInstallHook()
{
if (IsHookInstalled)
{
if (!API.UnhookWindowsHookEx(hHook))
throw new Win32Exception("Can't uninstall low level keyboard hook!");
hHook = IntPtr.Zero;
hModule = IntPtr.Zero;
hookInstall = false;
}
}
/// <summary>
/// Hook process messages.
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
static IntPtr HookProcFunction ( int nCode, IntPtr wParam, [In] IntPtr lParam ) {
if (nCode == 0) {
LLKHEventArgs args = null;
if (localHook) {
bool pressed = false;
if (lParam.ToInt32() >> 31 == 0)
pressed = true;
Keys keys = (Keys)wParam.ToInt32();
args = new LLKHEventArgs(keys, pressed, 0U, 0U);
if (pressed) {
if (KeyUp != null)
KeyUp(args);
} else {
if (KeyDown != null)
KeyDown(args);
}
} else {
KBDLLHOOKSTRUCT kbd = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
bool pressed = false;
if (wParam.ToInt32() == 0x100 || wParam.ToInt32() == 0x104)
pressed = true;
Keys keys = (Keys)kbd.vkCode;
args = new LLKHEventArgs(keys, pressed, kbd.time, kbd.scanCode);
if (pressed) {
if (KeyUp != null)
KeyUp(args);
} else {
if (KeyDown != null)
KeyDown(args);
}
}
if (args != null && args.Hooked)
return (IntPtr)1;
}
return API.CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
public class LLKHEventArgs
{
Keys keys;
bool pressed;
uint time;
uint scCode;
public LLKHEventArgs(Keys keys, bool pressed, uint time, uint scanCode)
{
this.keys = keys;
this.pressed = pressed;
this.time = time;
this.scCode = scanCode;
}
/// <summary>
/// Key.
/// </summary>
public Keys Keys
{ get { return keys; } }
/// <summary>
/// Is key pressed or no.
/// </summary>
public bool IsPressed
{ get { return pressed; } }
/// <summary>
/// The time stamp for this message, equivalent to what GetMessageTime would return for this message.
/// </summary>
public uint Time
{ get { return time; } }
/// <summary>
/// A hardware scan code for the key.
/// </summary>
public uint ScanCode
{ get { return scCode; } }
/// <summary>
/// Is user hook key.
/// </summary>
public bool Hooked { get; set; }
}
static class API
{
public delegate IntPtr HookProc(int nCode, IntPtr wParam, [In] IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, [In] IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn,
IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
}
}
Код:
using System.Windows.Forms;
using Hooks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace KeyboardHook
{
public partial class frmMain : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[Flags]
public enum MouseEventFlags : uint
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public frmMain()
{
InitializeComponent();
KBDHook.KeyDown += new KBDHook.HookKeyPress(KBDHook_KeyDown);
KBDHook.KeyUp += new KBDHook.HookKeyPress(KBDHook_KeyUp);
listBox1.Items.Clear();
KBDHook.LocalHook = false;
KBDHook.InstallHook();
}
void KBDHook_KeyUp(LLKHEventArgs e)
{
}
void KBDHook_KeyDown ( LLKHEventArgs e ) {
listBox1.Items.Add(e.Keys);
if (e.Keys.ToString() == "NumPad1")
{
label1.Text = "Привет!";
}
else
label1.Text = "";
if (label1.Text == "Привет!") // Если label == Привет тогда вызов сообщение! В будущем вместо вызова сообщение должно быть вызвано заранее прописаное действие с клавиатуры
{
MessageBox.Show("Работает!"); // Тут должно быть вызвано заранее прописаное действие с клавиатуры
}
listBox1.Items.RemoveAt(2); // Чистка listboxa после второго итема
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
KBDHook.UnInstallHook(); // Обязательно !!!
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
}
using Hooks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace KeyboardHook
{
public partial class frmMain : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[Flags]
public enum MouseEventFlags : uint
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
public frmMain()
{
InitializeComponent();
KBDHook.KeyDown += new KBDHook.HookKeyPress(KBDHook_KeyDown);
KBDHook.KeyUp += new KBDHook.HookKeyPress(KBDHook_KeyUp);
listBox1.Items.Clear();
KBDHook.LocalHook = false;
KBDHook.InstallHook();
}
void KBDHook_KeyUp(LLKHEventArgs e)
{
}
void KBDHook_KeyDown ( LLKHEventArgs e ) {
listBox1.Items.Add(e.Keys);
if (e.Keys.ToString() == "NumPad1")
{
label1.Text = "Привет!";
}
else
label1.Text = "";
if (label1.Text == "Привет!") // Если label == Привет тогда вызов сообщение! В будущем вместо вызова сообщение должно быть вызвано заранее прописаное действие с клавиатуры
{
MessageBox.Show("Работает!"); // Тут должно быть вызвано заранее прописаное действие с клавиатуры
}
listBox1.Items.RemoveAt(2); // Чистка listboxa после второго итема
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
KBDHook.UnInstallHook(); // Обязательно !!!
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
}