我想寫一個不管在什麼作用視窗都能
案F1執行程式
案F2建立文件檔
案F3關閉程式
我用KeyDown等等的指令都只能在VB上作業不能在其他視窗
2006-03-09 16:28:35 · 3 個解答 · 發問者 ? 1 in 電腦與網際網路 ➔ 程式設計
抱歉讓你回我這麼多,可是我主要要的功能只希望要知道怎麼在不管什麼過用視窗都能用按鍵控制鍵盤,就像輸入法那樣。
2006-03-12 16:48:41 · update #1
假設記事本是自己內部的一個視窗(Text)
利用API GetAsyncKeyState 來偵測熱鍵
以下程式碼
'--------------------------------------------------------------------------------------------------------------
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
If GetAsyncKeyState(112) <> 0 Then
Text.Show
End If
If GetAsyncKeyState(113) <> 0 Then
Text.Text1.Text = ""
End If
If GetAsyncKeyState(114) <> 0 Then
End
End If
End Sub
2006-03-17 15:44:21 · answer #1 · answered by ? 5 · 0⤊ 0⤋
這個問題必需使用到 Win32 API 才能解決,所以想先問你是否有在 VB 中寫過 Win32 API 的經驗
2006-03-09 21:28:23 · answer #2 · answered by Luke 4 · 0⤊ 0⤋
'假設按F1是啟動別的程式(如果是啟動自己我也不會),F3結束本身程式,程式須加上1個模組'以下放在模組Type EVENTMSG message As Long paramL As Long paramH As Long time As Long hWnd As LongEnd TypeDeclare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As LongDeclare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongDeclare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongDeclare Function UnhookWindowsHookEx Lib "user32" (ByVal H As Long) As LongPublic H As LongFunction HP(ByVal C As Long, ByVal W As Long, ByVal L As Long) As LongDim M As EVENTMSGIf C = 0 Then CopyMemory M, L, LenB(M) If M.message = &H100 Then Select Case M.paramH Case 59: Shell "Notepad", vbNormalFocus '啟動記事本 Case 60 '建立文件檔 Open "C:\Test.txt" For Output As #1 Print #1, "WJS" Close #1 Case 61: Unload Form1 '結束(不可用End,會當掉) End Select End IfEnd IfHP = CallNextHookEx(H, C, W, L)End Function'以下放在表單Private Sub Form_Load()H = SetWindowsHookEx(0, AddressOf HP, App.hInstance, 0)End SubPrivate Sub Form_Unload(Cancel As Integer)UnhookWindowsHookEx HEnd Sub
2006-03-09 17:47:51 · answer #3 · answered by W.J.S. 7 · 0⤊ 0⤋