How to detect pressing (Ctrl+C) by timer and API functions , Anywhere from another app

Mansour_Dalir 1,876 Reputation points
2023-09-27T11:04:23.9133333+00:00

In any other program, I pressed the key (Ctrl+C) to perform an operation. Thankful

GetAsyncKeyState

GetKeyboardState

  Public Declare Function GetAsyncKeystate Lib "user32"  (ByVal vKey As Long) As Integer
Public Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (pbKeyState As Byte) As Long
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,728 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 6,475 Reputation points
    2023-10-01T03:05:17.9333333+00:00

    This is Hotkey's class. Paste it on the form and use it.

    Hotkey.vb

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Hotkey1.Register(Keys.C, Hotkey.Modifiers.Control)
        End Sub
    
        Private Sub Hotkey1_HotkeyPress(sender As Object, e As Hotkey.HotkeyEventArgs) Handles Hotkey1.HotkeyPress
            TextBox1.AppendText(Now & " " & e.ToString() & vbCrLf)
        End Sub
    
    End Class
    

    When I set CTRL+C to Hotkey, I can no longer copy text. lol


2 additional answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,326 Reputation points Microsoft Vendor
    2023-09-28T09:03:32.7266667+00:00

    Hi @Mansour_Dalir ,

    You can use Windows API functions to implement global keyboard hooks. Here's an example

    https://gist.github.com/kirsbo/3b01a1412311e7a1d565

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. KOZ6.0 6,475 Reputation points
    2023-09-30T10:02:41.67+00:00

    Please take a sample. I'm cutting corners and needing a reference to System.Windows.Forms.

    LowLevelKeyboardHook.vb

    How to use:

    Public Class Form1
    
        Private WithEvents KbdHook As New LowLevelKeyboardHook
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            KbdHook.Start()
        End Sub
    
        Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
            KbdHook.Stop()
        End Sub
    
        Private Sub KbdHook_KeyDown(sender As Object, e As KeyEventArgs) Handles KbdHook.KeyDown
            If e.KeyCode = Keys.C AndAlso e.Control Then
                TextBox1.AppendText(Now & " CTRL + C KEYDOWN" & vbCrLf)
            End If
        End Sub
    
    End Class
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.