我現在沒有現成的代碼給你,不過可以給你一些建議。如果你想在同一進程內實現屏幕鍵盤,則你只需要再F2打開的時候,將F1的當前焦點控件對象作為參數,傳遞給F2窗體,在F2點擊屏幕控件時,設置該“控件.Text = 鍵值” 即可,這種我稱之為假屏幕鍵盤。如果你想跨進程實現屏幕鍵盤,就比較難些了(比如:你想用你的F2窗體上的按鍵,實現QQ聊天內容的輸入、記事本內容的輸入)。這個需要用到 “鍵盤鉤子”技術,有關這方面的資料,你不妨自己找找。 另外,你提的F2焦點老處理不好,我認為F2根本不需要焦點控件(不要用按鈕做控件,而實用Label或圖片就可以了。)
成都創(chuàng)新互聯(lián)公司成立10多年來,這條路我們正越走越好,積累了技術與客戶資源,形成了良好的口碑。為客戶提供成都網站設計、成都網站建設、網站策劃、網頁設計、空間域名、網絡營銷、VI設計、網站改版、漏洞修補等服務。網站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網站建設都非常重要,成都創(chuàng)新互聯(lián)公司通過對建站技術性的掌握、對創(chuàng)意設計的研究為客戶提供一站式互聯(lián)網解決方案,攜手廣大客戶,共同發(fā)展進步。
簡單點的可以用sendkeys,要模擬按鍵可以keybd_event,高級點的可以用sendmessage或者postmessage,我做了個自動喊話的,用的是sendkeys,下面是代碼:
Private
Declare
Function
FindWindow
Lib
"user32"
Alias
"FindWindowA"
(ByVal
lpClassName
As
String,
ByVal
lpWindowName
As
String)
As
Long
'
Private
Declare
Function
GetForegroundWindow
Lib
"user32"
()
As
Long
'獲得前臺窗口的句柄
Private
Declare
Function
GetAsyncKeyState
Lib
"user32"
(ByVal
vKey
As
Long)
As
Integer
'
Private
Sub
Form_Load()
Timer1.Enabled
=
False
End
Sub
Private
Sub
Timer1_Timer()
Dim
a,
b
As
Long
Static
t
As
Integer
a
=
FindWindow(vbNullString,
"地下城與勇士")
b
=
GetForegroundWindow
If
a
=
b
Then
t
=
t
+
1
If
t
=
1
Then
SendKeys
"{ENTER}"
ElseIf
t
=
2
Then
SendKeys
"{/}"
ElseIf
t
=
3
Then
SendKeys
"{r}"
ElseIf
t
=
4
Then
SendKeys
"{e}"
ElseIf
t
=
5
Then
SendKeys
"{ENTER}"
t
=
End
If
End
If
End
Sub
Private
Sub
Timer2_Timer()
If
GetAsyncKeyState(vbKeyF9)
Then
Timer1.Enabled
=
True
ElseIf
GetAsyncKeyState(vbKeyF10)
Then
Timer1.Enabled
=
False
a
=
End
If
End
Sub
tab是
"{TAB}".==========SendKeys
語句
將一個或多個按鍵消息發(fā)送到活動窗口,就如同在鍵盤上進行輸入一樣。語法SendKeys
string[,
wait]SendKeys
語句的語法具有以下幾個命名參數:部分
描述
string
必需的。字符串表達式,指定要發(fā)送的按鍵消息。
Wait
可選的。指定等待方式的
BooleandefBooleanDataType@veendf98.chm
值。如果為
False(缺省值),則控件在按鍵發(fā)送出去之后立刻返回到過程。如果為
True,則按鍵消息必須在控件返回到過程之前加以處理。
說明每個按鍵由一個或多個字符表示。為了指定單一鍵盤字符,必須按字符本身的鍵。例如,為了表示字母
A,可以用
"A"
作為
string。為了表示多個字符,就必須在字符后面直接加上另一個字符。例如,要表示
A、B
及
C,可用
"ABC"
作為
string。對
SendKeys
來說,加號
(+)、插入符
(^)、百分比符號
(%)、上劃線
(~)
及圓括號
(
)
都具有特殊意義。為了指定上述任何一個字符,要將它放在大括號
({})
當中。例如,要指定正號,可用
{+}
表示。方括號
([
])
對
SendKeys
來說并不具有特殊意義,但必須將它們放在大括號中。在其它應用程序中,方括號有特殊意義,在出現動態(tài)數據交換
(DDE)
的時候,它可能具有重要意義。為了指定大括號字符,請使用
{{}
及
{}}。為了在按下按鍵時指定那些不顯示的字符,例如
ENTER
或
TAB
以及那些表示動作而非字符的按鍵,請使用下列代碼:按鍵
代碼
BACKSPACE
{BACKSPACE},
{BS},
或
{BKSP}
BREAK
{BREAK}
CAPS
LOCK
{CAPSLOCK}
DEL
or
DELETE
{DELETE}
或
{DEL}
DOWN
ARROW
{DOWN}
END
{END}
ENTER
{ENTER}或
~
ESC
{ESC}
HELP
{HELP}
HOME
{HOME}
INS
or
INSERT
{INSERT}
或
{INS}
LEFT
ARROW
{LEFT}
NUM
LOCK
{NUMLOCK}
PAGE
DOWN
{PGDN}
PAGE
UP
{PGUP}
SCREEN
{PRTSC}
RIGHT
ARROW
{RIGHT}
SCROLL
LOCK
{SCROLLLOCK}
TAB
{TAB}
UP
ARROW
{UP}
F1
{F1}
F2
{F2}
F3
{F3}
F4
{F4}
F5
{F5}
F6
{F6}
F7
{F7}
F8
{F8}
F9
{F9}
F10
{F10}
F11
{F11}
F12
{F12}
F13
{F13}
F14
{F14}
F15
{F15}
F16
{F16}
為了指定那些與
SHIFT、CTRL
及
ALT
等按鍵結合的組合鍵,可在這些按鍵碼的前面放置一個或多個代碼,這些代碼列舉如下:按鍵
代碼
SHIFT
+
CTRL
^
ALT
%
為了說明在按下其它按鍵時應同時按下
SHIFT、CTRL、及
ALT
的任意組合鍵,請把那些按鍵的碼放在括號當中。例如,為了說明按下
E
與
C
的時候同時按下
SHIFT
鍵,請使用
"+(EC)"。為了說明在按下
E
的時候同時按下
SHIFT
鍵,但接著按
C
而不按
SHIFT,則使用
"+EC"。為了指定重復鍵,使用
{key
number}
的形式。必須在
key
與
number
之間放置一個空格。例如,{LEFT
42}
意指
42
次按下
LEFT
ARROW
鍵;{h
10}
則是指
10
次按下
H
鍵。注意
不能用
SendKeys
將按鍵消息發(fā)送到這樣一個應用程序,這個應用程序并沒有被設計成在
Microsoft
Windows
中運行。Sendkeys
也無法將
SCREEN
按鍵
{PRTSC}
發(fā)送到任何應用程序。
參考資料:MSDN
執(zhí)行鍵盤事件方法如下:
第一步,點擊VS工具。
第二步,打開后,新建一個Windows窗體應用程序。
第三步,請看右下方Form的屬性欄。
第四步,雙擊KeyDown。
第五步,輸入提示按鍵代碼,如圖所示。
第六步,再次輸入代碼,設置屬性KeyPreview為true。
第七步,運行程序,按下舉例F5,則會彈出提示,表明已經成功獲。
用API keybd_event來模擬按鍵過程,但是Ctrl+Alt+Del不能模擬,前面已經定義了各個按鍵的值,需要按什么鍵替換成對應的值就行了,由于是模擬鍵盤按鍵,所以按鍵發(fā)送的對象是當前激活的窗口。
Option Explicit
'
'Virtual Keys, Standard Set
'
Private Const VK_LBUTTON = H1
Private Const VK_RBUTTON = H2
Private Const VK_CANCEL = H3
Private Const VK_MBUTTON = H4 'NOT contiguous with L RBUTTON
Private Const VK_BACK = H8
Private Const VK_TAB = H9
Private Const VK_CLEAR = HC
Private Const VK_RETURN = HD
Private Const VK_SHIFT = H10
Private Const VK_CONTROL = H11
Private Const VK_MENU = H12
Private Const VK_PAUSE = H13
Private Const VK_CAPITAL = H14
Private Const VK_KANA = H15
Private Const VK_HANGEUL = H15 'old name - should be here for compatibility
Private Const VK_HANGUL = H15
Private Const VK_JUNJA = H17
Private Const VK_FINAL = H18
Private Const VK_HANJA = H19
Private Const VK_KANJI = H19
Private Const VK_ESCAPE = H1B
Private Const VK_CONVERT = H1C
Private Const VK_NONCONVERT = H1D
Private Const VK_ACCEPT = H1E
Private Const VK_MODECHANGE = H1F
Private Const VK_SPACE = H20
Private Const VK_PRIOR = H21
Private Const VK_NEXT = H22
Private Const VK_END = H23
Private Const VK_HOME = H24
Private Const VK_LEFT = H25
Private Const VK_UP = H26
Private Const VK_RIGHT = H27
Private Const VK_DOWN = H28
Private Const VK_SELECT = H29
Private Const VK_PRINT = H2A
Private Const VK_EXECUTE = H2B
Private Const VK_SNAPSHOT = H2C
Private Const VK_INSERT = H2D
Private Const VK_DELETE = H2E
Private Const VK_HELP = H2F
'VK_0 thru VK_9 are the same as ASCII '0' thru '9' (=H30 - =H39)
'VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (=H41 - =H5A)
Private Const VK_LWIN = H5B
Private Const VK_RWIN = H5C
Private Const VK_APPS = H5D
Private Const VK_NUMPAD0 = H60
Private Const VK_NUMPAD1 = H61
Private Const VK_NUMPAD2 = H62
Private Const VK_NUMPAD3 = H63
Private Const VK_NUMPAD4 = H64
Private Const VK_NUMPAD5 = H65
Private Const VK_NUMPAD6 = H66
Private Const VK_NUMPAD7 = H67
Private Const VK_NUMPAD8 = H68
Private Const VK_NUMPAD9 = H69
Private Const VK_MULTIPLY = H6A
Private Const VK_ADD = H6B
Private Const VK_SEPARATOR = H6C
Private Const VK_SUBTRACT = H6D
Private Const VK_DECIMAL = H6E
Private Const VK_DIVIDE = H6F
Private Const VK_F1 = H70
Private Const VK_F2 = H71
Private Const VK_F3 = H72
Private Const VK_F4 = H73
Private Const VK_F5 = H74
Private Const VK_F6 = H75
Private Const VK_F7 = H76
Private Const VK_F8 = H77
Private Const VK_F9 = H78
Private Const VK_F10 = H79
Private Const VK_F11 = H7A
Private Const VK_F12 = H7B
Private Const VK_F13 = H7C
Private Const VK_F14 = H7D
Private Const VK_F15 = H7E
Private Const VK_F16 = H7F
Private Const VK_F17 = H80
Private Const VK_F18 = H81
Private Const VK_F19 = H82
Private Const VK_F20 = H83
Private Const VK_F21 = H84
Private Const VK_F22 = H85
Private Const VK_F23 = H86
Private Const VK_F24 = H87
Private Const VK_NUMLOCK = H90
Private Const VK_SCROLL = H91
'
'VK_L* VK_R* - left and right Alt, Ctrl and Shift virtual keys.
'Used only as parameters to GetAsyncKeyState() and GetKeyState().
'No other API or message will distinguish left and right keys in this way.
'
Private Const VK_LSHIFT = HA0
Private Const VK_RSHIFT = HA1
Private Const VK_LCONTROL = HA2
Private Const VK_RCONTROL = HA3
Private Const VK_LMENU = HA4
Private Const VK_RMENU = HA5
'dwFlags
Private Const KEYEVENTF_EXTENDEDKEY = H1
Private Const KEYEVENTF_KEYUP = H2
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Sub Command1_Click()
Call keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Alt鍵
Call keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Print Screen鍵
Call keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Print Screen鍵
Call keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Alt鍵
Call keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Win鍵
Call keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Win鍵
Call keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Num Lock鍵
Call keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Num Lock鍵
End Sub
ivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SETTEXT = HC
Private Sub Command1_Click()
Dim hwnd As Long, Thwnd As Long
Shell "notepad.exe", 2 '打開一個記事本,將它最小化
hwnd = FindWindow("Notepad", "無標題 - 記事本") '得到記事本句柄
Thwnd = FindWindowEx(hwnd, 0, "Edit", vbNullString) '得到記事本句柄(就是我們寫字的那里)
SendMessage Thwnd, WM_SETTEXT, 0, ByVal "好了,快給我分" '向記事本寫入文字
End Sub