不使用 DLL 获取按键状态函数

发布于 2024-12-13 10:09:52 字数 685 浏览 1 评论 0原文

我需要 vb.net Getkeystate Function() 的代码,而不使用任何 DLL。我是从 vb 到 vb.net 的 API 转换的初学者。在 vb6 中,他们使用 user32 DLL。在 Vb.Net 中,我需要调用 Getkeystate 函数而不需要“ User32.dll”函数。谁能在不使用dll的情况下将代码发给我? 有没有办法在不使用 vb.net 中的 dll Getkeystate 函数的情况下获取按键状态?如果有任何人知道等效的方法,请告诉我该代码。该代码应该在 .net 框架中使用。

VB:

Private Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
Dim keystate As Long
keystate = GetKeyState(27)
MsgBox keystate

VB.net

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
On Error Resume Next
Dim KeyState As Long
KeyState = GetAsyncKeyState(17)
MsgBox(KeyState

I need codes for vb.net Getkeystate Function() without using any DLL.I'm beginner for API Conversion from vb to vb.net.In vb6 they using user32 DLL.In Vb.Net i need to call Getkeystate Function without that "User32.dll" function. can any one post me the codes without using dll?
Is there any way to get key state without using that dll Getkeystate function in vb.net..? if equivalent is any one know means show me that codes.that codes should be work with in .net framework.

vb:

Private Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
Dim keystate As Long
keystate = GetKeyState(27)
MsgBox keystate

vb.net

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
On Error Resume Next
Dim KeyState As Long
KeyState = GetAsyncKeyState(17)
MsgBox(KeyState

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素染倾城色 2024-12-20 10:09:52

您要么需要调用 Windows API,要么不需要 - 从您的问题中我不确定您是否理解使用 DLL/调用 Windows API 的含义。 User32.dll是Windows的一部分,调用其中的函数实际上是要求Windows为您执行函数。您想要替换该函数还是仅仅将调用转换为 vb.net?

如果您只需将调用转换为 vb.net,您可以使用 PInvoke 对 Windows API 的调用。

这是该页面的示例:

<DllImport("user32.dll", SetLastError := True, CharSet := CharSet.Unicode)> _
Private Function GetKeyState (ByVal nVirtKey As KeyStates) As Short
End Function

Imports System.Windows.Forms
Public Class MinhaNovaTextbox
    Inherits System.Windows.Forms.TextBox

    Dim bInserting As Boolean = True

    Private Declare Function GetKeyState _
        Lib "user32" (ByVal nVirtKey As Short) As Integer

    Public Sub New()
        MyBase.New()
        bInserting = GetKeyState(Keys.Insert)
    End Sub

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        bInserting = GetKeyState(Keys.Insert)
        If Not bInserting Then
            Me.SelectionLength = 1
        End If
        MyBase.OnKeyPress(e)
    End Sub
End Class

You either need to call the windows API or not - and I'm not sure from your question you understand what using the DLL/invoking the windows API means. User32.dll is part windows and calling a function within it is actually asking windows to perform function for you. Do you want to replace the function or merely convert the call to vb.net?

If all you need to just convert the call to vb.net you can use a PInvoke call to the windows API.

here's a sample from that page:

<DllImport("user32.dll", SetLastError := True, CharSet := CharSet.Unicode)> _
Private Function GetKeyState (ByVal nVirtKey As KeyStates) As Short
End Function

Imports System.Windows.Forms
Public Class MinhaNovaTextbox
    Inherits System.Windows.Forms.TextBox

    Dim bInserting As Boolean = True

    Private Declare Function GetKeyState _
        Lib "user32" (ByVal nVirtKey As Short) As Integer

    Public Sub New()
        MyBase.New()
        bInserting = GetKeyState(Keys.Insert)
    End Sub

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        bInserting = GetKeyState(Keys.Insert)
        If Not bInserting Then
            Me.SelectionLength = 1
        End If
        MyBase.OnKeyPress(e)
    End Sub
End Class
雨巷深深 2024-12-20 10:09:52

对于 17,Control (Ctrl) 键,请使用 My.Computer.Keyboard.CtrlKeyDown

您还可以使用 System.Windows.Forms.Control.ModifierKeys 或使用 Keyboard 类。但是,使用 My 命名空间要容易得多。

对于 27、Escape (Esc) 键,必须使用 Windows API 调用。请参阅 http://msdn.microsoft 上的 GetKeyState 函数。 com/en-us/library/ms646301.aspx

System.Windows.Form.Form 并不真正需要检查 Esc。只需添加一个“取消”按钮并设置表单的 CancelButton 属性即可。如果您需要更多控制,请处理 Key 事件之一; KeyUpKeyDownKeyPressPreviewKeyDown

For 17, the Control (Ctrl) key, use My.Computer.Keyboard.CtrlKeyDown.

You can also use System.Windows.Forms.Control.ModifierKeys or use the Keyboard class. But, it is much easier to use the My namespace.

For 27, The Escape (Esc) key, you must use the Windows API call. See GetKeyState function at http://msdn.microsoft.com/en-us/library/ms646301.aspx

Checking for Esc is not really needed with a System.Windows.Form.Form. Just add a "cancel" button and set the form's CancelButton property. If you need more control, then handle the one of the Key events; KeyUp, KeyDown, KeyPress, or PreviewKeyDown.

隐诗 2024-12-20 10:09:52
System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key)
System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文