By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

keyboard hook in c#

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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*********************@f14g2000cwb.googlegro ups.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

P: n/a
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.SendWait("^{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

P: n/a
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.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

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

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

}

static private void ThreadPorc()
{
Thread.Sleep(2000);
System.Media.SystemSounds.Asterisk.Play();
SendKeys.SendWait("^{BREAK}");

}

}
}
--

Stoitcho Goutsev (100)

<jp******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.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.SendWait("^{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

P: n/a
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

P: n/a
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*********************@z14g2000cwz.googlegro ups.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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.