Nicolas_75
New member
- Joined
- Feb 20, 2010
- Messages
- 2
- Programming Experience
- 5-10
Hi,
I would appreciate some help on the following issue.
My final goal is that striking F12 prints the date (through SendKeys) in the application/window having currently the focus (Word, Excel, Outlook, whatever). I have consequently implemented a globak keyboard hook (system-wide), which is running well (source code below).
My concern is that I would like to prevent any other local treatment based on F12. For instance, striking F12 under Microsotft Word prints the date is the open document (which is fine!) but also launches the "Save as..." dialog box, since F12 is a local shortcut/keybinding under Word. (Actually, the date is printed within a field of a the "Save as..." dialog box). I want to avoid this.
In other words, my aim is to improve the "hook" below so that striking F12 does what I want (which is already the case) but nothing more. That is to say, I would like that, except my treatment, the system "forgets" that F12 was striken.
Thanks in advance for your help!
Nicolas
I would appreciate some help on the following issue.
My final goal is that striking F12 prints the date (through SendKeys) in the application/window having currently the focus (Word, Excel, Outlook, whatever). I have consequently implemented a globak keyboard hook (system-wide), which is running well (source code below).
My concern is that I would like to prevent any other local treatment based on F12. For instance, striking F12 under Microsotft Word prints the date is the open document (which is fine!) but also launches the "Save as..." dialog box, since F12 is a local shortcut/keybinding under Word. (Actually, the date is printed within a field of a the "Save as..." dialog box). I want to avoid this.
In other words, my aim is to improve the "hook" below so that striking F12 does what I want (which is already the case) but nothing more. That is to say, I would like that, except my treatment, the system "forgets" that F12 was striken.
Thanks in advance for your help!
Nicolas
VB.NET:
' Option Strict On
Option Explicit On
' inspired by:
' http://jo0ls-dotnet-stuff.blogspot.com/2008/12/vbnet-global-keyboard-hook-to-detect.html
Imports System.Runtime.InteropServices
Public Class Form1
Private Const WH_KEYBOARD_LL As Integer = 13
Private Const WM_KEYUP As Integer = &H101
Private Const WM_SYSKEYUP As Integer = &H105
Private proc As LowLevelKeyboardProcDelegate = AddressOf HookCallback
Private hookID As IntPtr
Private Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
<DllImport("user32")> _
Private Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, _
ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
Sub New()
InitializeComponent()
Text = "KeyboardPlus 1.01"
hookID = SetHook(proc)
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
UnhookWindowsHookEx(hookID)
End Sub
Private Function SetHook(ByVal proc As LowLevelKeyboardProcDelegate) As IntPtr
Using curProcess As Process = Process.GetCurrentProcess()
Using curModule As ProcessModule = curProcess.MainModule
Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
End Using
End Using
End Function
Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
' "The WM_KEYUP message is posted to the window with the keyboard focus
' when a nonsystem key is released. A nonsystem key is a key that is pressed when the ALT key is not pressed,
' or a keyboard key that is pressed when a window has the keyboard focus."
If nCode >= 0 AndAlso (wParam.ToInt32 = WM_KEYUP OrElse wParam.ToInt32 = WM_SYSKEYUP) Then
Dim vkCode As Integer = Marshal.ReadInt32(lParam)
If vkCode = Keys.F12 Then
Dim dateOfTheDay As Char() = DateAndTime.Now.ToString.ToCharArray
If Console.CapsLock Then
SendKeys.Send("{CAPSLOCK}")
For index As Integer = 0 To 9
SendKeys.Send(dateOfTheDay(index))
Next
SendKeys.Send("{CAPSLOCK}")
Else
For index As Integer = 0 To 9
SendKeys.Send(dateOfTheDay(index))
Next
End If
End If
End If
Return CallNextHookEx(hookID, nCode, wParam, lParam)
End Function
End Class