2016-09-02 61 views
0

我試圖捕獲所有keystokes,並且我得到並且「無法在DLL'user32'中找到名爲'GetAsncKeyState'的入口點」錯誤 結果= GetAsncKeyState(I) 這裏是代碼無法在DLL'user32'中找到名爲'GetAsncKeyState'的入口點

Private Declare Function GetAsncKeyState Lib "user32" (ByVal vkey As Integer) As Integer 
Private Sub tmrKeys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrKeys.Tick 
    Dim result As Integer 
    Dim key As String 
    Dim i As Integer 
    For i = 2 To 90 
     result = 0 
     result = GetAsncKeyState(i) 
     If result = -3276 Then 
      key = Chr(i) 
      If i = 13 Then key = vbNewLine 
      Exit For 
     End If 
    Next i 
+0

我正在使用框架3.5,visualstudio 2010 – user5353093

+2

好奇你如何得到錯誤的名稱,請解釋這是怎麼發生的。 –

+0

@HansPassant是正確的,名稱是錯的... – Codexer

回答

1

你描述的錯誤的原因是因爲函數名稱GetAsyncKeyState,不GetAsncKeyState

而且在.NET你應該考慮使用DllImport屬性,而不是Declare聲明(這是一個過時的VB6時尚的方法)來provide more control over how platform invoke functions are called

這裏是低於GetAsyncKeyState聲明that I use,與包括XML文檔:

''' ---------------------------------------------------------------------------------------------------- 
''' <summary> 
''' Determines whether a key is up or down at the time the function is called, 
''' and whether the key was pressed after a previous call to <see cref="GetAsyncKeyState"/>. 
''' </summary> 
''' ---------------------------------------------------------------------------------------------------- 
''' <remarks> 
''' <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx"/> 
''' </remarks> 
''' ---------------------------------------------------------------------------------------------------- 
''' <param name="vKey"> 
''' The virtual-key code. 
''' <para></para> 
''' You can use left- and right-distinguishing constants to specify certain keys. 
''' <para></para> 
''' See Virtual-Key Codes: 
''' <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx"/> 
''' </param> 
''' ---------------------------------------------------------------------------------------------------- 
''' <returns> 
''' If the function succeeds, 
''' the return value specifies whether the key was pressed since the last call to <see cref="GetAsyncKeyState"/>, 
''' and whether the key is currently up or down. 
''' <para></para> 
''' If the most significant bit is set, the key is down, and if the least significant bit is set, 
''' the key was pressed after the previous call to <see cref="GetAsyncKeyState"/>. 
''' <para></para> 
''' However, you should not rely on this last behavior. 
''' <para></para> 
''' <para></para> 
''' The return value is zero for the following cases: 
''' <para></para> 
''' The current desktop is not the active desktop. 
''' <para></para> 
''' The foreground thread belongs to another process and the desktop does not allow the hook or the journal record. 
''' </returns> 
''' ---------------------------------------------------------------------------------------------------- 
<SuppressUnmanagedCodeSecurity> 
<DllImport("user32.dll", CharSet:=CharSet.Auto)> 
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys 
) As Short 
End Function 

注意:可以,如果你preffer的vKey參數更改爲Integer數據類型。

+0

我相信你的意思是:_「如果你喜歡」_「,你可以將'vKey'參數改爲'System.Windows.Forms.Keys'枚舉。 :) –

+0

哎呀,忘了upvote你的不錯的答案。 –

+0

@Visual Vincent在技術當然是,在所提供的答案的背景下沒有。感謝您的評論。 – ElektroStudios

相關問題