473,287 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 26464
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
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
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
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*********************@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
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
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....
2
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...
0
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...
0
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...
1
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...
1
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...
2
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
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...
2
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.