How do you Globally Hook the Keyboard in VB.net? 
Author Message
 How do you Globally Hook the Keyboard in VB.net?

I would like to be able to listen for keystrokes weather my app has
focus or not. Specifically, I would like to listen for [ctrl] + keystroke
and [alt] + keystroke type messages. Any help you could provide would be
greatly appreciated.

PJ



Quote:
> Perhaps there is a different way to achieve what you want to do.  Hooks
are
> generally not encouraged.

> What is it that you want to achieve in your app?  Perhaps we can find a
> different and "easier" way to solve you problem?

> thanks,

> Kathy Tamanaha (VB Test Team)

> --
> This posting is provided "AS IS" with no warranties, and confers no
rights.


> > I am trying desprately to have my app notified of keyboard messages when
> my
> > app does not have focus. Does anyone have sample code that does this? I
> have
> > tried using the Keyboard Hook API with no luck, the API returns null
when
> > executed. Any help would be appreciated. Here is the code.

> > TIA
> > PJ

> > Public Class Win32Hook

> > Private Declare Function SetWindowsHookEx Lib "user32" Alias
> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As
> KbdHookDelegate,
> > ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer

> > Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook
As
> > Integer) As Integer

> > Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As
> > Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam
As
> > Integer) As Integer

> > Public Delegate Function KbdHookDelegate(ByVal nCode As Integer, ByVal
> > wParam As Integer, ByVal lParam As Integer) As Integer

> > 'keybaord messages

> > Const WH_KEYBOARD = &H2

> > Const WH_KEYBOARD_LL = &H13

> > Const WM_KEYDOWN = &H100

> > Const WM_KEYUP = &H101

> > Const WH_MOUSE = &H7

> > Const WH_MOUSE_LL = &H14

> > Private _KeyboardHook As Integer

> > Public Function KbdHook(ByVal nCode As Integer, ByVal wParam As Integer,
> > ByVal lParam As Integer) As Integer

> > MsgBox("hi")

> > Return CallNextHookEx(_KeyboardHook, nCode, wParam, lParam)

> > End Function

> > Public Sub New()

> > If Not _KeyboardHook Then

> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KbdHook, 0,
0)

> > End If

> > End Sub

> > Protected Overrides Sub Finalize()

> > UnhookWindowsHookEx(_KeyboardHook)

> > MyBase.Finalize()

> > End Sub

> > End Class



Tue, 28 Dec 2004 22:51:40 GMT  
 How do you Globally Hook the Keyboard in VB.net?
I don't know if you want to go the Direct Input route, but
it should accomplish what you want.  There is some
tutorial code here (VB 6 Code)
http://msdn.microsoft.com/archive/default.asp?
url=/archive/en-us/dx8_vb/directx_vb/input_tut_vb_16ci.asp

There are a sample programs with the Dx8.1 SDK

Derek

Quote:
>-----Original Message-----
>I would like to be able to listen for keystrokes weather
my app has
>focus or not. Specifically, I would like to listen for
[ctrl] + keystroke
>and [alt] + keystroke type messages. Any help you could
provide would be
>greatly appreciated.

>PJ


message

>> Perhaps there is a different way to achieve what you
want to do.  Hooks
>are
>> generally not encouraged.

>> What is it that you want to achieve in your app?  

Perhaps we can find a
Quote:
>> different and "easier" way to solve you problem?

>> thanks,

>> Kathy Tamanaha (VB Test Team)

>> --
>> This posting is provided "AS IS" with no warranties,
and confers no
>rights.


>> > I am trying desprately to have my app notified of

keyboard messages when

- Show quoted text -

Quote:
>> my
>> > app does not have focus. Does anyone have sample code
that does this? I
>> have
>> > tried using the Keyboard Hook API with no luck, the
API returns null
>when
>> > executed. Any help would be appreciated. Here is the
code.

>> > TIA
>> > PJ

>> > Public Class Win32Hook

>> > Private Declare Function SetWindowsHookEx
Lib "user32" Alias
>> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal
lpfn As
>> KbdHookDelegate,
>> > ByVal hmod As Integer, ByVal dwThreadId As Integer)
As Integer

>> > Private Declare Function UnhookWindowsHookEx

Lib "user32" (ByVal hHook
Quote:
>As
>> > Integer) As Integer

>> > Private Declare Function CallNextHookEx Lib "user32"
(ByVal hHook As
>> > Integer, ByVal ncode As Integer, ByVal wParam As

Integer, ByVal lParam

- Show quoted text -

Quote:
>As
>> > Integer) As Integer

>> > Public Delegate Function KbdHookDelegate(ByVal nCode
As Integer, ByVal
>> > wParam As Integer, ByVal lParam As Integer) As Integer

>> > 'keybaord messages

>> > Const WH_KEYBOARD = &H2

>> > Const WH_KEYBOARD_LL = &H13

>> > Const WM_KEYDOWN = &H100

>> > Const WM_KEYUP = &H101

>> > Const WH_MOUSE = &H7

>> > Const WH_MOUSE_LL = &H14

>> > Private _KeyboardHook As Integer

>> > Public Function KbdHook(ByVal nCode As Integer, ByVal
wParam As Integer,
>> > ByVal lParam As Integer) As Integer

>> > MsgBox("hi")

>> > Return CallNextHookEx(_KeyboardHook, nCode, wParam,
lParam)

>> > End Function

>> > Public Sub New()

>> > If Not _KeyboardHook Then

>> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,

AddressOf KbdHook, 0,

- Show quoted text -

Quote:
>0)

>> > End If

>> > End Sub

>> > Protected Overrides Sub Finalize()

>> > UnhookWindowsHookEx(_KeyboardHook)

>> > MyBase.Finalize()

>> > End Sub

>> > End Class

>.



Wed, 29 Dec 2004 00:04:17 GMT  
 How do you Globally Hook the Keyboard in VB.net?
Derek, thanks for your response this is an interesting technique. Do you
know if this object supports events and can notify my app of key press
messages? I would like my app to be notified by the OS when key strokes have
occurred.

Again thanks,
PJ


Quote:
> I don't know if you want to go the Direct Input route, but
> it should accomplish what you want.  There is some
> tutorial code here (VB 6 Code)
> http://msdn.microsoft.com/archive/default.asp?
> url=/archive/en-us/dx8_vb/directx_vb/input_tut_vb_16ci.asp

> There are a sample programs with the Dx8.1 SDK

> Derek

> >-----Original Message-----
> >I would like to be able to listen for keystrokes weather
> my app has
> >focus or not. Specifically, I would like to listen for
> [ctrl] + keystroke
> >and [alt] + keystroke type messages. Any help you could
> provide would be
> >greatly appreciated.

> >PJ


> message

> >> Perhaps there is a different way to achieve what you
> want to do.  Hooks
> >are
> >> generally not encouraged.

> >> What is it that you want to achieve in your app?
> Perhaps we can find a
> >> different and "easier" way to solve you problem?

> >> thanks,

> >> Kathy Tamanaha (VB Test Team)

> >> --
> >> This posting is provided "AS IS" with no warranties,
> and confers no
> >rights.


> >> > I am trying desprately to have my app notified of
> keyboard messages when
> >> my
> >> > app does not have focus. Does anyone have sample code
> that does this? I
> >> have
> >> > tried using the Keyboard Hook API with no luck, the
> API returns null
> >when
> >> > executed. Any help would be appreciated. Here is the
> code.

> >> > TIA
> >> > PJ

> >> > Public Class Win32Hook

> >> > Private Declare Function SetWindowsHookEx
> Lib "user32" Alias
> >> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal
> lpfn As
> >> KbdHookDelegate,
> >> > ByVal hmod As Integer, ByVal dwThreadId As Integer)
> As Integer

> >> > Private Declare Function UnhookWindowsHookEx
> Lib "user32" (ByVal hHook
> >As
> >> > Integer) As Integer

> >> > Private Declare Function CallNextHookEx Lib "user32"
> (ByVal hHook As
> >> > Integer, ByVal ncode As Integer, ByVal wParam As
> Integer, ByVal lParam
> >As
> >> > Integer) As Integer

> >> > Public Delegate Function KbdHookDelegate(ByVal nCode
> As Integer, ByVal
> >> > wParam As Integer, ByVal lParam As Integer) As Integer

> >> > 'keybaord messages

> >> > Const WH_KEYBOARD = &H2

> >> > Const WH_KEYBOARD_LL = &H13

> >> > Const WM_KEYDOWN = &H100

> >> > Const WM_KEYUP = &H101

> >> > Const WH_MOUSE = &H7

> >> > Const WH_MOUSE_LL = &H14

> >> > Private _KeyboardHook As Integer

> >> > Public Function KbdHook(ByVal nCode As Integer, ByVal
> wParam As Integer,
> >> > ByVal lParam As Integer) As Integer

> >> > MsgBox("hi")

> >> > Return CallNextHookEx(_KeyboardHook, nCode, wParam,
> lParam)

> >> > End Function

> >> > Public Sub New()

> >> > If Not _KeyboardHook Then

> >> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,
> AddressOf KbdHook, 0,
> >0)

> >> > End If

> >> > End Sub

> >> > Protected Overrides Sub Finalize()

> >> > UnhookWindowsHookEx(_KeyboardHook)

> >> > MyBase.Finalize()

> >> > End Sub

> >> > End Class

> >.



Wed, 29 Dec 2004 00:24:20 GMT  
 How do you Globally Hook the Keyboard in VB.net?
Well, I was thinking you could just incorporate it into
your app, and then yes it could trap all keystrokes and
should be able to notify your program when certain
keystrokes are pressed.  Obviously, it could come at a
price because those same keystrokes could have
functionality in other apps.  If you are talking about
writing as a seperate background app that actually does
some sort of messaging to you program, then I don't know,
but you should be able to work that someway.

Derek  

Quote:
>-----Original Message-----
>Derek, thanks for your response this is an interesting
technique. Do you
>know if this object supports events and can notify my app
of key press
>messages? I would like my app to be notified by the OS

when key strokes have
Quote:
>occurred.

>Again thanks,
>PJ



>> I don't know if you want to go the Direct Input route,
but
>> it should accomplish what you want.  There is some
>> tutorial code here (VB 6 Code)
>> http://msdn.microsoft.com/archive/default.asp?
>> url=/archive/en-

us/dx8_vb/directx_vb/input_tut_vb_16ci.asp
Quote:

>> There are a sample programs with the Dx8.1 SDK

>> Derek

>> >-----Original Message-----
>> >I would like to be able to listen for keystrokes
weather
>> my app has
>> >focus or not. Specifically, I would like to listen for
>> [ctrl] + keystroke
>> >and [alt] + keystroke type messages. Any help you could
>> provide would be
>> >greatly appreciated.

>> >PJ


>> message

>> >> Perhaps there is a different way to achieve what you
>> want to do.  Hooks
>> >are
>> >> generally not encouraged.

>> >> What is it that you want to achieve in your app?
>> Perhaps we can find a
>> >> different and "easier" way to solve you problem?

>> >> thanks,

>> >> Kathy Tamanaha (VB Test Team)

>> >> --
>> >> This posting is provided "AS IS" with no warranties,
>> and confers no
>> >rights.


>> >> > I am trying desprately to have my app notified of
>> keyboard messages when
>> >> my
>> >> > app does not have focus. Does anyone have sample
code
>> that does this? I
>> >> have
>> >> > tried using the Keyboard Hook API with no luck, the
>> API returns null
>> >when
>> >> > executed. Any help would be appreciated. Here is
the
>> code.

>> >> > TIA
>> >> > PJ

>> >> > Public Class Win32Hook

>> >> > Private Declare Function SetWindowsHookEx
>> Lib "user32" Alias
>> >> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal
>> lpfn As
>> >> KbdHookDelegate,
>> >> > ByVal hmod As Integer, ByVal dwThreadId As Integer)
>> As Integer

>> >> > Private Declare Function UnhookWindowsHookEx
>> Lib "user32" (ByVal hHook
>> >As
>> >> > Integer) As Integer

>> >> > Private Declare Function CallNextHookEx
Lib "user32"
>> (ByVal hHook As
>> >> > Integer, ByVal ncode As Integer, ByVal wParam As
>> Integer, ByVal lParam
>> >As
>> >> > Integer) As Integer

>> >> > Public Delegate Function KbdHookDelegate(ByVal
nCode
>> As Integer, ByVal
>> >> > wParam As Integer, ByVal lParam As Integer) As
Integer

>> >> > 'keybaord messages

>> >> > Const WH_KEYBOARD = &H2

>> >> > Const WH_KEYBOARD_LL = &H13

>> >> > Const WM_KEYDOWN = &H100

>> >> > Const WM_KEYUP = &H101

>> >> > Const WH_MOUSE = &H7

>> >> > Const WH_MOUSE_LL = &H14

>> >> > Private _KeyboardHook As Integer

>> >> > Public Function KbdHook(ByVal nCode As Integer,
ByVal
>> wParam As Integer,
>> >> > ByVal lParam As Integer) As Integer

>> >> > MsgBox("hi")

>> >> > Return CallNextHookEx(_KeyboardHook, nCode, wParam,
>> lParam)

>> >> > End Function

>> >> > Publi{ w ??~x ~O4J
> c Sub New()

>> >> > If Not _KeyboardHook Then

>> >> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,
>> AddressOf KbdHook, 0,
>> >0)

>> >> > End If

>> >> > End Sub

>> >> > Protected Overrides Sub Finalize()

>> >> > UnhookWindowsHookEx(_KeyboardHook)

>> >> > MyBase.Finalize()

>> >> > End Sub

>> >> > End Class

>> >.

>.



Wed, 29 Dec 2004 01:07:47 GMT  
 How do you Globally Hook the Keyboard in VB.net?
Cameron, when I tried to open the sample I received an error stating that
the project was created with a newer version of Studio.net and was not
compatible with my version. Am I missing an update or something?

PJ



Quote:
> PJ

> I have attached a zip file with a simple project.
> The project has a single form. When the form loads it creates a listener
> class that inherits from "NativeWindow" which is used to intercept window
> messages sent to the application.
> I use a simple function called RegisterHotKey to setup the ALT or CTRL key
> combinations I want my app to listen for.
> I think this is pretty much what you have asked for.

> Note however that as far as I know there is no way to have an app receive
> key events unless you have registered the hotkey for your app. So if your
> app does not have focus a single keystroke will not be caught by the app.

> Hope that helps.

> Cameron McColl
> Microsoft



> Well, I was thinking you could just incorporate it into
> your app, and then yes it could trap all keystrokes and
> should be able to notify your program when certain
> keystrokes are pressed.  Obviously, it could come at a
> price because those same keystrokes could have
> functionality in other apps.  If you are talking about
> writing as a seperate background app that actually does
> some sort of messaging to you program, then I don't know,
> but you should be able to work that someway.

> Derek

> >-----Original Message-----
> >Derek, thanks for your response this is an interesting
> technique. Do you
> >know if this object supports events and can notify my app
> of key press
> >messages? I would like my app to be notified by the OS
> when key strokes have
> >occurred.

> >Again thanks,
> >PJ



> >> I don't know if you want to go the Direct Input route,
> but
> >> it should accomplish what you want.  There is some
> >> tutorial code here (VB 6 Code)
> >> http://msdn.microsoft.com/archive/default.asp?
> >> url=/archive/en-
> us/dx8_vb/directx_vb/input_tut_vb_16ci.asp

> >> There are a sample programs with the Dx8.1 SDK

> >> Derek

> >> >-----Original Message-----
> >> >I would like to be able to listen for keystrokes
> weather
> >> my app has
> >> >focus or not. Specifically, I would like to listen for
> >> [ctrl] + keystroke
> >> >and [alt] + keystroke type messages. Any help you could
> >> provide would be
> >> >greatly appreciated.

> >> >PJ


> >> message

> >> >> Perhaps there is a different way to achieve what you
> >> want to do.  Hooks
> >> >are
> >> >> generally not encouraged.

> >> >> What is it that you want to achieve in your app?
> >> Perhaps we can find a
> >> >> different and "easier" way to solve you problem?

> >> >> thanks,

> >> >> Kathy Tamanaha (VB Test Team)

> >> >> --
> >> >> This posting is provided "AS IS" with no warranties,
> >> and confers no
> >> >rights.


> >> >> > I am trying desprately to have my app notified of
> >> keyboard messages when
> >> >> my
> >> >> > app does not have focus. Does anyone have sample
> code
> >> that does this? I
> >> >> have
> >> >> > tried using the Keyboard Hook API with no luck, the
> >> API returns null
> >> >when
> >> >> > executed. Any help would be appreciated. Here is
> the
> >> code.

> >> >> > TIA
> >> >> > PJ

> >> >> > Public Class Win32Hook

> >> >> > Private Declare Function SetWindowsHookEx
> >> Lib "user32" Alias
> >> >> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal
> >> lpfn As
> >> >> KbdHookDelegate,
> >> >> > ByVal hmod As Integer, ByVal dwThreadId As Integer)
> >> As Integer

> >> >> > Private Declare Function UnhookWindowsHookEx
> >> Lib "user32" (ByVal hHook
> >> >As
> >> >> > Integer) As Integer

> >> >> > Private Declare Function CallNextHookEx
> Lib "user32"
> >> (ByVal hHook As
> >> >> > Integer, ByVal ncode As Integer, ByVal wParam As
> >> Integer, ByVal lParam
> >> >As
> >> >> > Integer) As Integer

> >> >> > Public Delegate Function KbdHookDelegate(ByVal
> nCode
> >> As Integer, ByVal
> >> >> > wParam As Integer, ByVal lParam As Integer) As
> Integer

> >> >> > 'keybaord messages

> >> >> > Const WH_KEYBOARD = &H2

> >> >> > Const WH_KEYBOARD_LL = &H13

> >> >> > Const WM_KEYDOWN = &H100

> >> >> > Const WM_KEYUP = &H101

> >> >> > Const WH_MOUSE = &H7

> >> >> > Const WH_MOUSE_LL = &H14

> >> >> > Private _KeyboardHook As Integer

> >> >> > Public Function KbdHook(ByVal nCode As Integer,
> ByVal
> >> wParam As Integer,
> >> >> > ByVal lParam As Integer) As Integer

> >> >> > MsgBox("hi")

> >> >> > Return CallNextHookEx(_KeyboardHook, nCode, wParam,
> >> lParam)

> >> >> > End Function

> >> >> > Publi{ w ??~x ~O4J
> > c Sub New()

> >> >> > If Not _KeyboardHook Then

> >> >> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,
> >> AddressOf KbdHook, 0,
> >> >0)

> >> >> > End If

> >> >> > End Sub

> >> >> > Protected Overrides Sub Finalize()

> >> >> > UnhookWindowsHookEx(_KeyboardHook)

> >> >> > MyBase.Finalize()

> >> >> > End Sub

> >> >> > End Class

> >> >.

> >.



Sat, 01 Jan 2005 19:03:40 GMT  
 How do you Globally Hook the Keyboard in VB.net?
Sorry, I should have mentioned that I am running version 7.0.9466 of .net
and version 1.0.3705 of the .Net framework.

PJ



Quote:
> PJ

> I have attached a zip file with a simple project.
> The project has a single form. When the form loads it creates a listener
> class that inherits from "NativeWindow" which is used to intercept window
> messages sent to the application.
> I use a simple function called RegisterHotKey to setup the ALT or CTRL key
> combinations I want my app to listen for.
> I think this is pretty much what you have asked for.

> Note however that as far as I know there is no way to have an app receive
> key events unless you have registered the hotkey for your app. So if your
> app does not have focus a single keystroke will not be caught by the app.

> Hope that helps.

> Cameron McColl
> Microsoft



> Well, I was thinking you could just incorporate it into
> your app, and then yes it could trap all keystrokes and
> should be able to notify your program when certain
> keystrokes are pressed.  Obviously, it could come at a
> price because those same keystrokes could have
> functionality in other apps.  If you are talking about
> writing as a seperate background app that actually does
> some sort of messaging to you program, then I don't know,
> but you should be able to work that someway.

> Derek

> >-----Original Message-----
> >Derek, thanks for your response this is an interesting
> technique. Do you
> >know if this object supports events and can notify my app
> of key press
> >messages? I would like my app to be notified by the OS
> when key strokes have
> >occurred.

> >Again thanks,
> >PJ



> >> I don't know if you want to go the Direct Input route,
> but
> >> it should accomplish what you want.  There is some
> >> tutorial code here (VB 6 Code)
> >> http://msdn.microsoft.com/archive/default.asp?
> >> url=/archive/en-
> us/dx8_vb/directx_vb/input_tut_vb_16ci.asp

> >> There are a sample programs with the Dx8.1 SDK

> >> Derek

> >> >-----Original Message-----
> >> >I would like to be able to listen for keystrokes
> weather
> >> my app has
> >> >focus or not. Specifically, I would like to listen for
> >> [ctrl] + keystroke
> >> >and [alt] + keystroke type messages. Any help you could
> >> provide would be
> >> >greatly appreciated.

> >> >PJ


> >> message

> >> >> Perhaps there is a different way to achieve what you
> >> want to do.  Hooks
> >> >are
> >> >> generally not encouraged.

> >> >> What is it that you want to achieve in your app?
> >> Perhaps we can find a
> >> >> different and "easier" way to solve you problem?

> >> >> thanks,

> >> >> Kathy Tamanaha (VB Test Team)

> >> >> --
> >> >> This posting is provided "AS IS" with no warranties,
> >> and confers no
> >> >rights.


> >> >> > I am trying desprately to have my app notified of
> >> keyboard messages when
> >> >> my
> >> >> > app does not have focus. Does anyone have sample
> code
> >> that does this? I
> >> >> have
> >> >> > tried using the Keyboard Hook API with no luck, the
> >> API returns null
> >> >when
> >> >> > executed. Any help would be appreciated. Here is
> the
> >> code.

> >> >> > TIA
> >> >> > PJ

> >> >> > Public Class Win32Hook

> >> >> > Private Declare Function SetWindowsHookEx
> >> Lib "user32" Alias
> >> >> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal
> >> lpfn As
> >> >> KbdHookDelegate,
> >> >> > ByVal hmod As Integer, ByVal dwThreadId As Integer)
> >> As Integer

> >> >> > Private Declare Function UnhookWindowsHookEx
> >> Lib "user32" (ByVal hHook
> >> >As
> >> >> > Integer) As Integer

> >> >> > Private Declare Function CallNextHookEx
> Lib "user32"
> >> (ByVal hHook As
> >> >> > Integer, ByVal ncode As Integer, ByVal wParam As
> >> Integer, ByVal lParam
> >> >As
> >> >> > Integer) As Integer

> >> >> > Public Delegate Function KbdHookDelegate(ByVal
> nCode
> >> As Integer, ByVal
> >> >> > wParam As Integer, ByVal lParam As Integer) As
> Integer

> >> >> > 'keybaord messages

> >> >> > Const WH_KEYBOARD = &H2

> >> >> > Const WH_KEYBOARD_LL = &H13

> >> >> > Const WM_KEYDOWN = &H100

> >> >> > Const WM_KEYUP = &H101

> >> >> > Const WH_MOUSE = &H7

> >> >> > Const WH_MOUSE_LL = &H14

> >> >> > Private _KeyboardHook As Integer

> >> >> > Public Function KbdHook(ByVal nCode As Integer,
> ByVal
> >> wParam As Integer,
> >> >> > ByVal lParam As Integer) As Integer

> >> >> > MsgBox("hi")

> >> >> > Return CallNextHookEx(_KeyboardHook, nCode, wParam,
> >> lParam)

> >> >> > End Function

> >> >> > Publi{ w ??~x ~O4J
> > c Sub New()

> >> >> > If Not _KeyboardHook Then

> >> >> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,
> >> AddressOf KbdHook, 0,
> >> >0)

> >> >> > End If

> >> >> > End Sub

> >> >> > Protected Overrides Sub Finalize()

> >> >> > UnhookWindowsHookEx(_KeyboardHook)

> >> >> > MyBase.Finalize()

> >> >> > End Sub

> >> >> > End Class

> >> >.

> >.



Sat, 01 Jan 2005 19:06:26 GMT  
 How do you Globally Hook the Keyboard in VB.net?
Well, I am still perplexed by the error message, but I opened you source
code up in notepad and copied it into my app. The code works just as I
wanted it to. Thanks for you time and effort.

PJ



Quote:
> PJ

> I have attached a zip file with a simple project.
> The project has a single form. When the form loads it creates a listener
> class that inherits from "NativeWindow" which is used to intercept window
> messages sent to the application.
> I use a simple function called RegisterHotKey to setup the ALT or CTRL key
> combinations I want my app to listen for.
> I think this is pretty much what you have asked for.

> Note however that as far as I know there is no way to have an app receive
> key events unless you have registered the hotkey for your app. So if your
> app does not have focus a single keystroke will not be caught by the app.

> Hope that helps.

> Cameron McColl
> Microsoft



> Well, I was thinking you could just incorporate it into
> your app, and then yes it could trap all keystrokes and
> should be able to notify your program when certain
> keystrokes are pressed.  Obviously, it could come at a
> price because those same keystrokes could have
> functionality in other apps.  If you are talking about
> writing as a seperate background app that actually does
> some sort of messaging to you program, then I don't know,
> but you should be able to work that someway.

> Derek

> >-----Original Message-----
> >Derek, thanks for your response this is an interesting
> technique. Do you
> >know if this object supports events and can notify my app
> of key press
> >messages? I would like my app to be notified by the OS
> when key strokes have
> >occurred.

> >Again thanks,
> >PJ



> >> I don't know if you want to go the Direct Input route,
> but
> >> it should accomplish what you want.  There is some
> >> tutorial code here (VB 6 Code)
> >> http://msdn.microsoft.com/archive/default.asp?
> >> url=/archive/en-
> us/dx8_vb/directx_vb/input_tut_vb_16ci.asp

> >> There are a sample programs with the Dx8.1 SDK

> >> Derek

> >> >-----Original Message-----
> >> >I would like to be able to listen for keystrokes
> weather
> >> my app has
> >> >focus or not. Specifically, I would like to listen for
> >> [ctrl] + keystroke
> >> >and [alt] + keystroke type messages. Any help you could
> >> provide would be
> >> >greatly appreciated.

> >> >PJ


> >> message

> >> >> Perhaps there is a different way to achieve what you
> >> want to do.  Hooks
> >> >are
> >> >> generally not encouraged.

> >> >> What is it that you want to achieve in your app?
> >> Perhaps we can find a
> >> >> different and "easier" way to solve you problem?

> >> >> thanks,

> >> >> Kathy Tamanaha (VB Test Team)

> >> >> --
> >> >> This posting is provided "AS IS" with no warranties,
> >> and confers no
> >> >rights.


> >> >> > I am trying desprately to have my app notified of
> >> keyboard messages when
> >> >> my
> >> >> > app does not have focus. Does anyone have sample
> code
> >> that does this? I
> >> >> have
> >> >> > tried using the Keyboard Hook API with no luck, the
> >> API returns null
> >> >when
> >> >> > executed. Any help would be appreciated. Here is
> the
> >> code.

> >> >> > TIA
> >> >> > PJ

> >> >> > Public Class Win32Hook

> >> >> > Private Declare Function SetWindowsHookEx
> >> Lib "user32" Alias
> >> >> > "SetWindowsHookExA" (ByVal idHook As Integer, ByVal
> >> lpfn As
> >> >> KbdHookDelegate,
> >> >> > ByVal hmod As Integer, ByVal dwThreadId As Integer)
> >> As Integer

> >> >> > Private Declare Function UnhookWindowsHookEx
> >> Lib "user32" (ByVal hHook
> >> >As
> >> >> > Integer) As Integer

> >> >> > Private Declare Function CallNextHookEx
> Lib "user32"
> >> (ByVal hHook As
> >> >> > Integer, ByVal ncode As Integer, ByVal wParam As
> >> Integer, ByVal lParam
> >> >As
> >> >> > Integer) As Integer

> >> >> > Public Delegate Function KbdHookDelegate(ByVal
> nCode
> >> As Integer, ByVal
> >> >> > wParam As Integer, ByVal lParam As Integer) As
> Integer

> >> >> > 'keybaord messages

> >> >> > Const WH_KEYBOARD = &H2

> >> >> > Const WH_KEYBOARD_LL = &H13

> >> >> > Const WM_KEYDOWN = &H100

> >> >> > Const WM_KEYUP = &H101

> >> >> > Const WH_MOUSE = &H7

> >> >> > Const WH_MOUSE_LL = &H14

> >> >> > Private _KeyboardHook As Integer

> >> >> > Public Function KbdHook(ByVal nCode As Integer,
> ByVal
> >> wParam As Integer,
> >> >> > ByVal lParam As Integer) As Integer

> >> >> > MsgBox("hi")

> >> >> > Return CallNextHookEx(_KeyboardHook, nCode, wParam,
> >> lParam)

> >> >> > End Function

> >> >> > Publi{ w ??~x ~O4J
> > c Sub New()

> >> >> > If Not _KeyboardHook Then

> >> >> > _KeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,
> >> AddressOf KbdHook, 0,
> >> >0)

> >> >> > End If

> >> >> > End Sub

> >> >> > Protected Overrides Sub Finalize()

> >> >> > UnhookWindowsHookEx(_KeyboardHook)

> >> >> > MyBase.Finalize()

> >> >> > End Sub

> >> >> > End Class

> >> >.

> >.



Sat, 01 Jan 2005 19:26:59 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How do you globally hook the keyboard?

2. Windows Keyboard Hook in .NET

3. Mhcallback - Keyboard hook and Mouse hook , HOW!

4. Please Help - System Keyboard Hook using VB

5. Help with VB keyboard hooking !

6. keyboard api hooks for vb ??

7. GotFocus LostFocus done globally?

8. Hooking into the Clipboard chain in VB.Net?

9. How to hook VB.Net into Visual Source Safe

10. System wide hook for Mouse and Keyboard events

11. KeyBoard Hook & Class

12. Keyboard / Mouse Hook: "Decoding" wParam, lParam

 

 
Powered by phpBB® Forum Software