- Codice: Seleziona tutto
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" ( _
ByVal cChar As Byte) As Integer
Private Declare Function VkKeyScanW Lib "user32" ( _
ByVal cChar As Integer) As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Public Sub KeyDown(ByVal vKey As KeyCodeConstants)
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
End Sub
Public Sub KeyUp(ByVal vKey As KeyCodeConstants)
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub
Public Function KeyCode(ByVal sChar As String) As KeyCodeConstants
Dim bNt As Boolean
Dim iKeyCode As Integer
Dim b() As Byte
Dim iKey As Integer
Dim vKey As KeyCodeConstants
Dim iShift As ShiftConstants
' Determine if we have Unicode support or not:
bNt = ((GetVersion() And &H80000000) = 0)
' Get the keyboard scan code for the character:
If (bNt) Then
b = sChar
CopyMemory iKey, b(0), 2
iKeyCode = VkKeyScanW(iKey)
Else
b = StrConv(sChar, vbFromUnicode)
iKeyCode = VkKeyScan(b(0))
End If
KeyCode = (iKeyCode And &HFF&)
End Function
Private Sub Command1_Click()
Text1.SetFocus
KeyDown (VBKeyN) '(oppure invece di VBKeyN metto 78) schiaccia e tiene premuto N nella textbox
End Sub
Ora il mio problema: io devo simulare la pressione dei tasti ALT + F4 (x chiudere una finestra): x l'F4 no problem, VBKeyf4 e funziona, il mio problema invece è la pressione dell'alt (che per chiudere una finestra deve essere l'Alt di sinistra...). Sul SITO sono dati tutti i codici (anche maiuscolo, ctrl, tab, ecc...), ma non si parla dell'alt... Provando con la funzione normale di vb form_keydown(keycode as integer, shift as integer) e facendomi dare il keycode, schiacciando l'alt mi restituisce 18, ma poi se lo metto nella funzione x schiacciarlo non me l oschiaccia: qualcuno sa come fare???? Grazie mille!
![Brindisi [brindisi]](http://www.megalab.it/forum/images/smilies/cheers.gif)
![Approvazione [^]](http://www.megalab.it/forum/images/smilies/Oh-yea.gif)