|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am trying to send the keystroke for the control key in VB,
sendkeys as far as I can see dosn't let you just have the ctrl key. The reason for this is that I have a KVM that switches with a ctrl crtl on the keyboard, and I want to simulate this. |
|
#2
|
|||
|
|||
|
U can use hotkey to replace "sendkey" function.U may custom a hotkey on the keyboard to call some function!
A hotkey is a key combination that you can use to trigger an action anywhere in Windows. When the form loads, use the RegisterHotKey API function to install the hotkey (in this example, Alt-F10). Then subclass to watch for the WM_HOTKEY message. Private Sub Form_Load() ' Register the hotkey. If RegisterHotKey(hWnd, HOTKEY_ID, _ MOD_ALT, VK_F10) = 0 _ Then MsgBox "Error registering hotkey." Unload Me End If ' Subclass the TextBox to watch for ' WM_HOTKEY messages. OldWindowProc = SetWindowLong( _ hWnd, GWL_WNDPROC, _ AddressOf NewWindowProc) End Sub When the new WindowProc sees the WM_HOTKEY message, it calls the form's public Hotkey subroutine. ' Look for the WM_HOTKEY message. Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg _ As Long, ByVal wParam As Long, ByVal lParam As Long) As _ Long Const WM_NCDESTROY = &H82 Const WM_HOTKEY = &H312 ' If we're being destroyed, ' restore the original WindowProc and ' unregister the hotkey. If Msg = WM_NCDESTROY Then SetWindowLong _ hWnd, GWL_WNDPROC, _ OldWindowProc UnregisterHotKey hWnd, HOTKEY_ID End If ' See if this is the WM_HOTKEY message. If Msg = WM_HOTKEY Then Form1.Hotkey ' Process the message normally. NewWindowProc = CallWindowProc( _ OldWindowProc, hWnd, Msg, wParam, _ lParam) End Function When the program receives the Hotkey event, it uses the GetForegroundWindow API function to find the window in the foreground. It then call SetWindowPlacement to minimize the window. ' We got the hotkey. Public Sub Hotkey() Dim active_hwnd As Long Dim wp As WINDOWPLACEMENT ' Get the active window's handle. active_hwnd = GetForegroundWindow() ' Get the window's current placement information. wp.length = Len(wp) GetWindowPlacement active_hwnd, wp ' Minimize it. wp.showCmd = SW_SHOWMINIMIZED ' Perform the action. SetWindowPlacement active_hwnd, wp End Sub |
|
#3
|
|||
|
|||
|
hotkey
I'm not sure thats what I'm looking for.
Here are more specifics of what I'm trying to do, I want my app to send the keystrokes , ctrl ctrl, when the mouse touches the edge of the screen, since sendkey doesn't have an option to just send the ctrl key, only crtl+something else I cannot use sendkey, Hotkey seems to create a keystroke listener from which an action can be taken, but my listener is a mouse listener and the action is the keystroke. How would you use hotkeys to send out a keystroke? Any Ideas? |
|
#4
|
|||
|
|||
|
I have known your meaning.U can use win32api "Sendinput".This api can realize this function:The SendInput function synthesizes keystrokes, mouse motions, and button clicks!This is the detailed information about it:
Decalration: Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long, pInputs As GENERALINPUT, ByVal cbSize As Long) As Long Parameters: ?nInputs [in] Specifies the number of structures in the pInputs array. ?pInputs [in] Pointer to an array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream. ?cbSize [in] Specifies the size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function will fail. Return Value: The function returns the number of events that it successfully inserted into the keyboard or mouse input stream. If the function returns zero, the input was already blocked by another thread. To get extended error information, call GetLastError. Sample: 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() 'KPD-Team 2000 'URL: http://www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net '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 U can visit http://www.allapi.net/ and down API-Guide contained all content. |
|
#5
|
|||
|
|||
|
to send the ctrl key, it's "^", so ctrl R would be "^R"
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > sending ctrl keystroke in VB |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|