pressing a hotkey while the program is not focused(minimized)

12/13/2015 12:31 iontzel222#1
Hello guys i want to do a simple program it could be usefull to me i want to make a textbox and insert a text in it then copy it via button and paste via hotkey
i have written the code and done it but the problem in the hotkey is working only inside the Form not outside as i intend to do can someone help me since i am a begginer in coding ? :D

Quote:
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy.Click
If TextBox1.SelectedText <> "" Then
Clipboard.SetText(TextBox1.SelectedText)
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim iData As IDataObject = Clipboard.GetDataObject()
If iData.GetDataPresent(DataFormats.Text) Then
TextBox1.SelectedText = CType(iData.GetData(DataFormats.Text), String)
Else
MsgBox(")
End If
End Sub

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F8 Then
Button2.PerformClick()
End If
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub

End Class
12/13/2015 13:00 BeginnerDO#2
You need to register the hotkey(s) you want to use. Here is an example :)

PHP Code:

Imports System
.Runtime.InteropServices

Public Class Form1
    
Public Const WM_HOTKEY As Integer = &H312

    
<DllImport("User32.dll")> _
    
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr_
                        ByVal id 
As IntegerByVal fsModifiers As Integer_
                        ByVal vk 
As Integer) As Integer
    End 
Function

    <
DllImport("User32.dll")> _
    
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr_
                        ByVal id 
As Integer) As Integer
    End 
Function

    Private 
Sub Form1_Load(sender As System.ObjectAs System.EventArgsHandles MyBase.Load
        RegisterHotKey
(Me.Handle1000Keys.F8)
    
End Sub

    
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If 
m.Msg WM_HOTKEY Then
            Dim id 
As IntPtr m.WParam
            Select 
Case (id.ToString)
                Case 
"100"
                    
MsgBox("lol")
            
End Select
        End 
If
        
MyBase.WndProc(m)
    
End Sub
End 
Class 
12/13/2015 13:53 iontzel222#3
:D:D great thank you very much instead the code in button 2 i used this
If iData.GetDataPresent(DataFormats.Text) Then
SendKeys.Send("^v")
:) guess that works too thank you again
12/14/2015 20:13 iBanq#4
There's a very simple method.
Just use the GetAsyncKeyState-API:

PHP Code:
Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Keys) As Short 
to use it just put this into a button or timer:

PHP Code:
If GetAsyncKeyState(ASCII Code of the Key) = -32767 Then 
[Only registered and activated users can see links. Click Here To Register...] a link of ASCII keys F1-F12.
12/20/2015 15:13 anonymous-29742#5
Code:
'Under Public Class Form1
Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Keys) As Short

'Put this in a timer or in a Loop
If GetAsyncKeyState(Keys.F8) = -32767 Then
'Do the stuff what you want to do
End If