Click here to Skip to main content
15,861,125 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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
Comments
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.
—SA

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

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649052%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms649033%28v=vs.85%29.aspx[^].

You can use this method via P/Invoke. This is already done for you:
http://www.pinvoke.net/default.aspx/user32.setclipboardviewer[^],
http://www.pinvoke.net/default.aspx/user32/AddClipboardFormatListener.html?diff=y[^].

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.)

—SA
 
Share this answer
 
What I think you are looking for is the Keypress event.
Look that word up here.
Or this one.
http://vb.net-informations.com/gui/key-press-vb.htm[^]

or here

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.90).aspx[^]
Unless you Just had to use those keys,why not use a context Menu Strip from the tool box.
Or just write the code.

http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip(v=vs.90).aspx[^]


Then it is a matter of using the clipboard. "My.Computer.Clipboard."
http://msdn.microsoft.com/en-us/library/a2kwzzs8(v=vs.90).aspx[^]
 
Share this answer
 
Comments
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 my.computer.clipboard 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:
' http://jo0ls-dotnet-stuff.blogspot.com/2008/12/vbnet-global-keyboard-hook-to-detect.html

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 <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.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
Inside the hook callback you can call GetKeyState[^] to get the status of the modifer keys.

The rest is some clever coding.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900