Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Using the Windows Keyboard Hook
|
533
Discussion
The hooks provided by the Windows operating system allow for very powerful code
to be written with a minimum of work. The hook used in this recipe is the
WH_
KEYBOARD
hook, which watches messages that are generated by the keyboard.
// Handle Previous Track keyboard button here.
textBox1.Text += "Media Previous Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_STOP:
// Handle Stop keyboard button here.
textBox1.Text += "Media Stop key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PLAY_PAUSE:
// Handle Play keyboard button here.
textBox1.Text += "Media Play/Pause key caught" +
Environment.NewLine;
break;
}
}
return (CallNextHookEx(hookHandle, code, wparam, lparam));
}
// Click event handlers for button1 and button2.
private void button1_Click(object sender, System.EventArgs e)
{
// Set the keyboard hook.
if (hookHandle == 0)
{
cb = new HookProc(Proc);
hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0,
AppDomain.GetCurrentThreadId( ));
}
else
{
textBox1.Text += "Hook already set" + Environment.NewLine;
}
textBox1.Text += "Start: " + hookHandle + Environment.NewLine;
}
private void button2_Click(object sender, System.EventArgs e)
{
// Unhook the keyboard hook.
textBox1.Text += "End: " + UnhookWindowsHookEx(hookHandle) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
C# Cookbook

C# Cookbook

Joe Mayo
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata