473,597 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keyboard hook in c#

Hi,

I am tryin to create a keyboard hook that sends the keystroke ctrl +
pause/break.

I haven't used keyboard hooks before so I'm not too sure how to use
them

public int MyKeyboardProc( int nCode, int wParam, int lParam)
{
// perform operation

}

I have to implement this method in order to hook the keyboard but I am
not sure what the parameters are ,like the virtual keycode for WParam
not sure if you specify this or put in one that windows already
recognises.

Any help would be appreciated

Thanks

James

Feb 15 '06 #1
7 26525
jpierson,

I don't quite understand what you are trying to achieve here.

1. Keyboard hooks are for listening keyboard events not for sending them.
2. If you talk about global keyboard hools, they are not supported by .NET
which means that you need to use PInvoke to use them.
3. If you want to intercept keys to your appplication you can do it in
different ways.
4. If you want to send keys you can use the SendKeys class.

Either ways it is not just a simple MyKeyboardProc method
--

Stoitcho Goutsev (100)

<jp******@gmail .com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi,

I am tryin to create a keyboard hook that sends the keystroke ctrl +
pause/break.

I haven't used keyboard hooks before so I'm not too sure how to use
them

public int MyKeyboardProc( int nCode, int wParam, int lParam)
{
// perform operation

}

I have to implement this method in order to hook the keyboard but I am
not sure what the parameters are ,like the virtual keycode for WParam
not sure if you specify this or put in one that windows already
recognises.

Any help would be appreciated

Thanks

James

Feb 15 '06 #2
Hi Stoitcho,

I would use the sendkeys class I'm using it to send other commands but
it won't work with the ctrl + pause/break sequence

SendKeys.SendWa it("^{BREAK}")

The reason I am using a keyboard hook is because it's the only way I
can think of that the ctrl + pause/break key sequence will work every
time. the method I put up is just a simple method part of the keyboard
hook procedure i don't know how to implement it referring to the ctrl
+ pause/break key sequence thats what i'm asking?.

It's nothing to do with a global hook I just want the
key sequence to work within the application and as you point out global
hooks are not supported in .net

Thanks

James

Feb 15 '06 #3
Hi,

I'm not an expert in the area, but I but up a small test application and
from what I can see I'm able to set Ctrl+Break key combination to my the
application.
I still can't understand how you intend to use keyboard hooks to send keys.
Just for information gloabal low-level keyboard hooks are possible with
..NET.

Here is the code form my test applciation. The application starts printing
to the console in an infinite loop. Some time after the start from a worker
thread I send Ctrl+Break. This terminates the console application. I tested
in .NET 2.0 I don't know if there are problems with .NET1.x

using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Windows. Forms;
using System.Threadin g;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{

int i = 0;
Thread t = new Thread(new ThreadStart(Thr eadPorc));
t.Start();
while (true)
{
Console.Write(i ++);
}

}

static private void ThreadPorc()
{
Thread.Sleep(20 00);
System.Media.Sy stemSounds.Aste risk.Play();
SendKeys.SendWa it("^{BREAK}") ;

}

}
}
--

Stoitcho Goutsev (100)

<jp******@gmail .com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Hi Stoitcho,

I would use the sendkeys class I'm using it to send other commands but
it won't work with the ctrl + pause/break sequence

SendKeys.SendWa it("^{BREAK}")

The reason I am using a keyboard hook is because it's the only way I
can think of that the ctrl + pause/break key sequence will work every
time. the method I put up is just a simple method part of the keyboard
hook procedure i don't know how to implement it referring to the ctrl
+ pause/break key sequence thats what i'm asking?.

It's nothing to do with a global hook I just want the
key sequence to work within the application and as you point out global
hooks are not supported in .net

Thanks

James

Feb 15 '06 #4
Hi Stoitcho ,

I have something similar already the reason I am using a keyboard hook
is that ctrl + pause/break is a special key sequence and it won't work
just using the send keys class.

I have to hook it to the application in order for it to be recognised i
just don't know how to implement it properly in order for it to work
with my application.

Thanks

James

Feb 16 '06 #5
jpierson,
Sorry, but I don't know if I can help you with that. Frankly I don't
understand your problem. If you could post some simple compilable code that
somehow demostrates your problem that would probably be the best. As far as
I can see I'm the only one that keep posting in your thread probably the
rest in the ng don't understand the problem as well.

There are two types of keyboard hooks: WH_KEYBOARD and WH_KEYBOARD_LL.

WH_KEYBOARD_LL is possible to implement in .NET. This is a golbal hook and
it intercepts all key events before they to be dispatched to any
application. I don't think you are interested in that.

WH_KEYBOARD intercepts WM_KEYDOWN and WM_KEYUP messages as they are
retreived form the message queue of a UI thread.
This keyboard hook can be implemented in .NET only to hook on threads
created by the same process as the hook procedure code. In .NET you cannot
set this hook gloabally, neither you can hook on to a thread created in
different process. I'm not sure though bout the latter, never tried it;
maybe you hook on to a thread in a process that runs the CLR; it might be
possible I don't know, but frankly I doubt it.

It looks more that you are after WM_KEYBOARD hook. As I said you can use it
to hook only threads in your process, so I was thinking isn't it possible to
do it differently then (for example with message loop filters or overriding
WndProc method.

Anyways, if you havent read this MSDN doc about hooks yet, take a look at
it
http://msdn.microsoft.com/library/de...usinghooks.asp

You can find there the skeleton of a WH_KEYBOARD procedure. It is native
code, but translation to C# must be really straightforward .

When you implement the skeleton then upon HC_ACTION you can check the wParam
for the virtual codes of the Break and Ctrl keys. The codes are the same as
Keys.Pause and Keys.ControlKey .
In the hook you are going to receive info from the raw WM_KEYDOWN and
WM_KEYUP that means you need to listen for pressing first the Ctrl key, set
a flag internally and then wait for the Break key. You won't receive one
combined code for Ctrl+Break. Whether the key is pressed or released you can
find your form the 31th bit in the lParam: 1. the key is released and 0 the
key is pressed.

Keep in mind thought that you need to check the *code* for PM_NOREMOVE also.
If the code is PM_NOREMOVE that means the WM_KEY* message stays in the
message queue and you will probably receive one more notification for the
same key. This happens when the message luup only peeks for a message.

I hope you'll find this information helpful, but again my advice is to keep
hooks only as a last option. Settings hooks requires P/Invoke which has to
be avoided in managed applications.
--
HTH
Stoitcho Goutsev (100)

<jp******@gmail .com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hi Stoitcho ,

I have something similar already the reason I am using a keyboard hook
is that ctrl + pause/break is a special key sequence and it won't work
just using the send keys class.

I have to hook it to the application in order for it to be recognised i
just don't know how to implement it properly in order for it to work
with my application.

Thanks

James

Feb 16 '06 #6
Hi Stoitcho,

sorry I only seen this now so I didnt reply sooner.
Thanks for your reply.

I am using an alternative way of doing it instead I'm changing the baud
rate then simulating pressing the space bar for 10-15 seconds.
There used to be a bug in some terminal emulators and this is how they
used to get around it.

The reason i was using a keyboard hook was to hook it to an application
because it would not work the normal way ,as it works in the code you
put up.

I can't see the difficulty in understanding it ,I think i explained it
pretty simply.I'm tryin to send a break sequennce ctrl + pause/break
but it will not work normally so thats why I was trying to hook it to
the application so it would work everytime.I was not using a keyboard
hook to send it at all, I was trying to hook it to the application so
it would send a break everytime,using sendkeys().

Thanks for your help

James

Mar 6 '06 #7
The confusion probably arises because you want to send keyboard input but are
discussing a keyboard hook, which is used to receive/monitor keyboard input.

As you point out, SendKeys doesn't work with every key. To send Ctrl+Pause,
use the SendInput user32.dll function or the keybd_event function. Although
keybd_event is listed as "superceded " in WinXP, we find it to be the most
reliable function for sending keyboard input across all International
keyboard layouts.

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com
"jp******@gmail .com" wrote:
Hi Stoitcho,

sorry I only seen this now so I didnt reply sooner.
Thanks for your reply.

I am using an alternative way of doing it instead I'm changing the baud
rate then simulating pressing the space bar for 10-15 seconds.
There used to be a bug in some terminal emulators and this is how they
used to get around it.

The reason i was using a keyboard hook was to hook it to an application
because it would not work the normal way ,as it works in the code you
put up.

I can't see the difficulty in understanding it ,I think i explained it
pretty simply.I'm tryin to send a break sequennce ctrl + pause/break
but it will not work normally so thats why I was trying to hook it to
the application so it would work everytime.I was not using a keyboard
hook to send it at all, I was trying to hook it to the application so
it would send a break everytime,using sendkeys().

Thanks for your help

James

May 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13004
by: daFritz | last post by:
Hi! I need to prevent Task-Switching in my App, so I tried to implement a low level keyboard hook in a extra class. But it seems that the Parameters are not passed correctly to my Hook function. Similar Examples in VB .NET work correctly. Heres the Source Code: using System; using System.Runtime.InteropServices;
2
5427
by: Christoph Brüser | last post by:
Hi, in my application I want to react to certain keys when a context menu is showing. So I installed a keyboard hook, but now whenever a key is pressed when the menu is showing, the application crashes and sometimes throws a NullReferenceException in system.windows.forms.dll. I assume that the hook somehow messes up the context menu's message loop or something like this. I also had this problem with a mouse hook, so i guess its a...
0
1390
by: BobAchgill | last post by:
When I try to hook a non letter key like left arrow (37) all is OK. If (Hookstruct.vkCode = 37) Or (Hookstruct.vkCode = 83) And CBool(Hookstruct.flags) Then But, for example, the hook for the letter "s" key (83) does not work. Mysteriously, the "s" starts hooking properly after I set and continue through a break point. I can even then remove the breakpoint and the "s" continues to hook OK as long as I have the debug session going.
0
2607
by: Hema | last post by:
hi all, i have implemented a keyboard hook which is active only when IE is open. Its a BHO. I am adding the code below: using System; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Web.Services; using Microsoft.Win32; using SHDocVw;
1
6807
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the message queue, regardless of what application has focus. I can identify the keystrokes and input device by registering for raw input (RegisterRawInputDevices) and processing the WM_INPUT message. This gives me the keystrokes and the ability to...
1
11206
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the message queue, regardless of what application has focus. I can identify the keystrokes and input device by registering for raw input (RegisterRawInputDevices) and processing the WM_INPUT message. This gives me the keystrokes and the ability to...
2
1426
by: Frank | last post by:
In a dialog box procedure is there a way to determine if keyboard input is from the keypad or the arrow keys? I need to know if the input is from the main keyboard keys. Thanks
2
4416
by: bizcor | last post by:
Is it possible to do a local keyboard hook to a process running in a new thread. The process is a command line application and is instanciated when a button is pressed on the main form. I have had the hook working Globally, but this means that the hook logs keystokes from the main app as well as the keystrokes fom the CMD process. The hook is WH_KEYBOARD. I have tryed passing the threadId into setkeyhook and thence to SetWindowsHookEx but...
2
5972
by: Justin | last post by:
So I have a keyboard hook that I have implemeted into my c# app. I need to not allow any hotkey actions to be performed when my app is opened. I can capture the key events and handle them if I want to. The only problem is that I have fields that require input. Do I have to programmatically add the characters to the input fields when I am handling the event. I don't want these keys sent to the system. But only to my program. Or does...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8035
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3886
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.