Click here to Skip to main content
12,404,895 members (69,867 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
I know there are already many resources on this topic however I have some other questions that are related.

Okay so my first question is about system wide hot keys, how is this achieved exactly because I haven't been able to find a working solution. I need to be able to press [Ctrl + Alt + V] for example to show a context menu.

My second question is about the context menu it's self and the clipboard, I want the user to be able to click an item in the context menu and it be pasted into the active application; is there a way to do this as I haven't yet seen one?

Thanks for any help
Posted 21-Mar-13 11:26am
Sergey Alexandrovich Kryukov 21-Mar-13 20:08pm
Why doing all that? You did not get any sensible answer so far, but capturing of the keyboard input globally is quite possible. The question is: why?
And the menu is per application; you cannot capture it. But you can add a global keyboard viewer.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

To start with, you can add your custom keyboard viewer and a keyboard format listener.[^],[^].

You can use this method via P/Invoke. This is already done for you:[^],[^].

See other clipboard Windows API (first two link, on the left).

If you need something else, you can also add a global hook and capture keyboard input. I don't think you would need it, but I don't know your goals exactly. (It's the best to start your questions from explaining your ultimate goals.)

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What I think you are looking for is the Keypress event.
Look that word up here.
Or this one.[^]

or here[^]
Unless you Just had to use those keys,why not use a context Menu Strip from the tool box.
Or just write the code.[^]

Then it is a matter of using the clipboard. "My.Computer.Clipboard."[^]
Kieran Crown 21-Mar-13 18:55pm
The content you provided isn't what I'm looking for as the key events only fire when the form has focus and I'm looking for a system wide solution and the function doesn't allow for pasting into a application but rather just setting the clipboard data.
Dave Kreskowiak 21-Mar-13 19:01pm
Google for "VB.NET Global Keyboard Hook". It's not exactly easy to implement, but it's what you're looking for.

As for pasting into the active application, that isn't active anymore when the user starts clicking around in your app. Your app becomes the active application.
Kieran Crown 21-Mar-13 19:04pm
Okay so what do current Clipboard Managers use?
Kieran Crown 21-Mar-13 19:25pm
Okay, I found this. It works for one key press, how would I manage to find or modify this one to work with key combos?

Option Explicit On

' inspired by:

Imports System.Runtime.InteropServices

Public Class Clip_Tray

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 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()
Text = "KeyboardPlus 1.01"
hookID = SetHook(proc)
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
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.Alt Then
If vkCode = Keys.A Then

End If
End If
End If
Return CallNextHookEx(hookID, nCode, wParam, lParam)
End Function
ledtech3 21-Mar-13 20:34pm
Actually if you try a simple test of 2 textboxes on a form and No ContextMenuStrip you can copy prepopulated text from the first textbox and right click and paste to the Second.

Or if you add a ContextMenuStrip then enable it in the control you want it to paste to you could do something like this in the Click event.
Private Sub PasteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem1.Click
Dim pasteString As String = My.Computer.Clipboard.GetText
TextBox1.Text = pasteString
End Sub
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Inside the hook callback you can call GetKeyState[^] to get the status of the modifer keys.

The rest is some clever coding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100