游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3541|回复: 9

API系列:一些有关键盘的API实例

 关闭 [复制链接]

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
发表于 2006-9-27 17:38:00 | 显示全部楼层 |阅读模式
请看
常用的有关键盘的函数有这些:
Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer

Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long

Declare Function GetKeyboardState Lib "user32" Alias "GetKeyboardState" (pbKeyState As Byte) As Long

Declare Function GetKeyboardType Lib "user32" Alias "GetKeyboardType" (ByVal nTypeFlag As Long) As Long

Declare Function GetKeyState Lib "user32" Alias "GetKeyState" (ByVal nVirtKey As Long) As Integer

Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutA" (ByVal pwszKLID As String, ByVal flags As Long) As Long

Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long

Declare Function SetKeyboardState Lib "user32" Alias "SetKeyboardState" (lppbKeyState As Byte) As Long

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:38:00 | 显示全部楼层

Key Spy

'In a module
Public Const DT_CENTER = &H1
Public Const DT_WORDBREAK = &H10
Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, ByVal lpDrawTextParams As Any) As Long
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Global Cnt As Long, sSave As String, sOld As String, Ret As String
Dim Tel As Long
Function GetPressedKey() As String
    For Cnt = 32 To 128
        'Get the keystate of a specified key
        If GetAsyncKeyState(Cnt) <> 0 Then
            GetPressedKey = Chr$(Cnt)
            Exit For
        End If
    Next Cnt
End Function
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
    Ret = GetPressedKey
    If Ret <> sOld Then
        sOld = Ret
        sSave = sSave + sOld
    End If
End Sub

'In a form
Private Sub Form_Load()
    Me.Caption = "Key Spy"
    'Create an API-timer
    SetTimer Me.hwnd, 0, 1, AddressOf TimerProc
End Sub
Private Sub Form_Paint()
    Dim R As RECT
    Const mStr = "Start this project, go to another application, type something, switch back to this application and unload the form. If you unload the form, a messagebox with all the typed keys will be shown."
    'Clear the form
    Me.Cls
    'API uses pixels
    Me.ScaleMode = vbPixels
    'Set the rectangle's values
    SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight
    'Draw the text on the form
    DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0&
End Sub
Private Sub Form_Resize()
    Form_Paint
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Kill our API-timer
    KillTimer Me.hwnd, 0
    'Show all the typed keys
    MsgBox sSave
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:38:00 | 显示全部楼层

Keyboard Type

Const KL_NAMELENGTH = 9
Const KT_TYPE = 0
Const KT_SUBTYPE = 1
Const KT_FUNCTIONKEYS = 2
Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long
Private Declare Function GetKeyboardType Lib "user32" (ByVal nTypeFlag As Long) As Long
Private Sub Form_Paint()
    Dim strName As String
    'Clear the form
    Me.Cls
    'Create a buffer
    strName = String(KL_NAMELENGTH, 0)
    'Get the keyboard layout name
    GetKeyboardLayoutName strName
    Me.Print "Keyboard layout name: " + strName
    Select Case GetKeyboardType(KT_TYPE)
        Case 1
            Me.Print "Keyboard type: IBM PC/XT or compatible (83-key) keyboard"
        Case 2
            Me.Print "Keyboard type: Olivetti ?ICO?(102-key) keyboard"
        Case 3
            Me.Print "Keyboard type: IBM PC/AT (84-key) or similar keyboard"
        Case 4
            Me.Print "Keyboard type: IBM enhanced (101- or 102-key) keyboard"
        Case 5
            Me.Print "Keyboard type: Nokia 1050 and similar keyboards"
        Case 6
            Me.Print "Keyboard type: Nokia 9140 and similar keyboards"
        Case 7
            Me.Print "Keyboard type: Japanese keyboard"
        Case Else7
            Me.Print "Keyboard type: Unknown"
    End Select
    Select Case GetKeyboardType(KT_FUNCTIONKEYS)
        Case 1
            Me.Print "Number of function keys: 10"
        Case 2
            Me.Print "Number of function keys: 12 (sometimes 18)"
        Case 3
            Me.Print "Number of function keys: 10"
        Case 4
            Me.Print "Number of function keys: 12"
        Case 5
            Me.Print "Number of function keys: 10"
        Case 6
            Me.Print "Number of function keys: 24"
        Case 7
            Me.Print "Number of function keys: Hardware dependent and specified by the OEM"
        Case Else
            Me.Print "Number of function keys: Unknown"
    End Select
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:39:00 | 显示全部楼层

LoadKeyboardLayout

'This fucntion changes the locale and as a result, the keyboardlayout gets adjusted

'parameters for api's
Const KL_NAMELENGTH As Long = 9                      'length of the keyboardbuffer
Const KLF_ACTIVATE  As Long = &H1                     'activate the layout

'the language constants
Const LANG_NL_STD As String = "00000413"
Const LANG_EN_US As String = "00000409"
Const LANG_DU_STD As String = "00000407"
Const LANG_FR_STD As String = "0000040C"

'api's to adjust the keyboardlayout
Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long
Private Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutA" (ByVal pwszKLID As String, ByVal flags As Long) As Long
Public Function SetKbLayout(strLocaleId As String) As Boolean
    'Changes the KeyboardLayout
    'Returns TRUE when the KeyboardLayout was adjusted properly, FALSE otherwise
    'If the KeyboardLayout isn't installed, this function will install it for you
    On Error Resume Next
    Dim strLocId As String 'used to retrieve current KeyboardLayout
    Dim strMsg As String   'used as buffer
    Dim lngErrNr As Long   'receives the API-error number

  'create a buffer
  strLocId = String(KL_NAMELENGTH, 0)
  'retrieve the current KeyboardLayout
  GetKeyboardLayoutName strLocId
  'Check whether the current KeyboardLayout and the
  'new one are the same
  If strLocId = (strLocaleId & Chr(0)) Then
    'If they're the same, we return immediately
    SetKbLayout = True
  Else
    'create buffer
    strLocId = String(KL_NAMELENGTH, 0)
    'load and activate the layout for the current thread
    strLocId = LoadKeyboardLayout((strLocaleId & Chr(0)), KLF_ACTIVATE)
    If IsNull(strLocId) Then  'returns NULL when it fails
      SetKbLayout = False
    Else 'check again
      'create buffer
      strLocId = String(KL_NAMELENGTH, 0)
      'retrieve the current layout
      GetKeyboardLayoutName strLocId
      If strLocId = (strLocaleId & Chr(0)) Then
        SetKbLayout = True
      Else
        SetKbLayout = False
      End If
    End If
  End If
End Function
Private Sub Form_Load()
    'change the current keybour layout to 'English - US'
    SetKbLayout LANG_EN_US
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:39:00 | 显示全部楼层

Keyboard Animation

Const VK_CAPITAL = &H14
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_USED = VK_SCROLL
Private Type KeyboardBytes
     kbByte(0 To 255) As Byte
End Type
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
Private Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim kbArray As KeyboardBytes, CapsLock As Boolean, kbOld As KeyboardBytes
Private Sub Form_Load()
    'Get the current keyboardstate
    GetKeyboardState kbOld
    'Hide the form
    Me.Hide
    MsgBox "Keep your eyes on the little num-, shift- and scrolllock lights on the keyboard."
    TurnOff VK_CAPITAL
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 1000
    TurnOn VK_NUMLOCK
    Sleep 100
    TurnOn VK_CAPITAL
    Sleep 100
    TurnOn VK_SCROLL
    Sleep 300
    TurnOff VK_NUMLOCK
    Sleep 100
    TurnOff VK_CAPITAL
    Sleep 100
    TurnOff VK_SCROLL
    Sleep 500
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_CAPITAL
    Sleep 200
    TurnOff VK_CAPITAL
    Sleep 200
    TurnOn VK_CAPITAL
    Sleep 200
    TurnOff VK_CAPITAL
    Sleep 200
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_NUMLOCK
    TurnOn VK_SCROLL
    Sleep 200
    TurnOff VK_NUMLOCK
    TurnOff VK_SCROLL
    Sleep 200
    TurnOn VK_CAPITAL
    Sleep 400
    TurnOff VK_CAPITAL
    Sleep 200
    TurnOn VK_NUMLOCK
    Sleep 100
    TurnOn VK_CAPITAL
    Sleep 100
    TurnOn VK_SCROLL
    Sleep 300
    TurnOff VK_SCROLL
    Sleep 100
    TurnOff VK_CAPITAL
    Sleep 100
    TurnOff VK_NUMLOCK
    Sleep 1000
    Unload Me
End Sub
Private Sub TurnOn(vkKey As Long)
    'Get the keyboard state
    GetKeyboardState kbArray
    'Change a key
    kbArray.kbByte(vkKey) = 1
    'Set the keyboard state
    SetKeyboardState kbArray
End Sub
Private Sub TurnOff(vkKey As Long)
    'Get the keyboard state
    GetKeyboardState kbArray
    'change a key
    kbArray.kbByte(vkKey) = 0
    'set the keyboard state
    SetKeyboardState kbArray
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'restore the old keyboard state
    SetKeyboardState kbOld
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:40:00 | 显示全部楼层

Hook

'In a module
Public Const WH_KEYBOARD = 2
Public Const VK_SHIFT = &H10
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public hHook As Long
Public Function KeyboardProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'if idHook is less than zero, no further processing is required
    If idHook < 0 Then
        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    Else
        'check if SHIFT-S is pressed
        If (GetKeyState(VK_SHIFT) And &HF0000000) And wParam = Asc("S") Then
            'show the result
            Form1.Print "Shift-S pressed ..."
        End If
        'call the next hook
        KeyboardProc = CallNextHookEx(hHook, idHook, wParam, ByVal lParam)
    End If
End Function

'In a form, called Form1
Private Sub Form_Load()
    'set a keyboard hook
    hHook = SetWindowsHookEx(WH_KEYBOARD, AddressOf KeyboardProc, App.hInstance, App.ThreadID)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'remove the windows-hook
    UnhookWindowsHookEx hHook
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:41:00 | 显示全部楼层

keyb_event

Const VK_H = 72
Const VK_E = 69
Const VK_L = 76
Const VK_O = 79
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Form_KeyPress(KeyAscii As Integer)
    'Print the key on the form
    Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
    'Clear the form
    Me.Cls
    keybd_event VK_H, 0, 0, 0   ' press H
    keybd_event VK_H, 0, KEYEVENTF_KEYUP, 0   ' release H
    keybd_event VK_E, 0, 0, 0  ' press E
    keybd_event VK_E, 0, KEYEVENTF_KEYUP, 0  ' release E
    keybd_event VK_L, 0, 0, 0  ' press L
    keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0  ' release L
    keybd_event VK_L, 0, 0, 0  ' press L
    keybd_event VK_L, 0, KEYEVENTF_KEYUP, 0  ' release L
    keybd_event VK_O, 0, 0, 0  ' press O
    keybd_event VK_O, 0, KEYEVENTF_KEYUP, 0  ' release O
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:41:00 | 显示全部楼层

SendInput

Const VK_H = 72
Const VK_E = 69
Const VK_L = 76
Const VK_O = 79
Const KEYEVENTF_KEYUP = &H2
Const INPUT_MOUSE = 0
Const INPUT_KEYBOARD = 1
Const INPUT_HARDWARE = 2
Private Type MOUSEINPUT
  dx As Long
  dy As Long
  mouseData As Long
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type KEYBDINPUT
  wVk As Integer
  wScan As Integer
  dwFlags As Long
  time As Long
  dwExtraInfo As Long
End Type
Private Type HARDWAREINPUT
  uMsg As Long
  wParamL As Integer
  wParamH As Integer
End Type
Private Type GENERALINPUT
  dwType As Long
  xi(0 To 23) As Byte
End Type
Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Form_KeyPress(KeyAscii As Integer)
    'Print the key on the form
    Me.Print Chr$(KeyAscii);
End Sub
Private Sub Form_Paint()
    'Clear the form
    Me.Cls
    'call the SendKey-function
    SendKey VK_H
    SendKey VK_E
    SendKey VK_L
    SendKey VK_L
    SendKey VK_O
End Sub
Private Sub SendKey(bKey As Byte)
    Dim GInput(0 To 1) As GENERALINPUT
    Dim KInput As KEYBDINPUT
    KInput.wVk = bKey  'the key we're going to press
    KInput.dwFlags = 0 'press the key
    'copy the structure into the input array's buffer.
    GInput(0).dwType = INPUT_KEYBOARD   ' keyboard input
    CopyMemory GInput(0).xi(0), KInput, Len(KInput)
    'do the same as above, but for releasing the key
    KInput.wVk = bKey  ' the key we're going to realease
    KInput.dwFlags = KEYEVENTF_KEYUP  ' release the key
    GInput(1).dwType = INPUT_KEYBOARD  ' keyboard input
    CopyMemory GInput(1).xi(0), KInput, Len(KInput)
    'send the input now
    Call SendInput(2, GInput(0), Len(GInput(0)))
End Sub

187

主题

6490

帖子

6491

积分

论坛元老

团长

Rank: 8Rank: 8

积分
6491
 楼主| 发表于 2006-9-27 17:44:00 | 显示全部楼层

Re:API系列:一些有关键盘的API实例

就是这些。

16

主题

71

帖子

77

积分

注册会员

Rank: 2

积分
77
发表于 2006-9-27 21:48:00 | 显示全部楼层

Re:API系列:一些有关键盘的API实例

日 你改饿从膳了 啊哈哈哈啊
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 12:44

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表