Vb.net Capture Ctrl+C

后端 未结 2 1450
自闭症患者
自闭症患者 2021-01-17 03:15

I want to capture when someone uses CtrlC even when off focus. im using Visual Basic 2010.

2条回答
  •  时光取名叫无心
    2021-01-17 03:56

    You need to create a low-level hook. This CodeProject example works perfect and I have used it myself for learning purposes. I modified the code slighty, but here is a simple example of something you could do with that library. Again, this is just an example and may not reflect the final code, but could be easily modified to capture Control+C, and the library is well documented.

       private static bool m_ControlKeyPressed = false;
    
       private static void ControlC_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyValue == 162 || e.KeyValue == 163) {
                m_ControlKeyPressed = true;
            }
            if (m_ControlKeyPressed) {
                if (e.KeyCode == Keys.C) {
                    e.SuppressKeyPress = true; // Suppress, or do something with it
                }
            }
        }
    
        private static void ControlC_KeyUp(object sender, KeyEventArgs e) {
            if (m_ControlKeyPressed) {
                 if (e.KeyValue == 162 || e.KeyValue == 163) {
                    m_ControlKeyPressed = false;
                }
            }
        }
    

    Conversion to Vb.Net

    Private Shared m_ControlKeyPressed As Boolean = False
    
    Private Shared Sub ControlC_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
            m_ControlKeyPressed = True
        End If
        If m_ControlKeyPressed Then
            If e.KeyCode = Keys.C Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub
    
    Private Shared Sub ControlC_KeyUp(sender As Object, e As KeyEventArgs)
        If m_ControlKeyPressed Then
            If e.KeyValue = 162 OrElse e.KeyValue = 163 Then
                m_ControlKeyPressed = False
            End If
        End If
    End Sub
    

提交回复
热议问题