Страница: 1 |
|
Вопрос: Перехват нажатия клавиш - vba
|
Добавлено: 01.09.09 15:23
|
|
Автор вопроса: AngryBadger
|
Всем привет. А подскажите плз, как на vba реализовать перехват нажатия клавиш на клавиатуре (если находишься на рабочем листе)? Интересует нажатие стрелок и пробела.
Ответить
|
Номер ответа: 1 Автор ответа: AngryBadger
Вопросов: 33 Ответов: 245
|
Профиль | | #1
|
Добавлено: 01.09.09 16:55
|
Опять разобрался сам)) Если кому-то нужно пользуйтесь!
- Option Explicit
-
- Public Declare Function CallNextHookEx Lib "user32" _
- (ByVal hHook As Long, _
- ByVal nCode As Long, _
- ByVal wParam As Long, _
- ByVal lParam As Long) As Long
-
- Public Declare Function UnhookWindowsHookEx Lib "user32" _
- (ByVal hHook As Long) As Long
-
- Public 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
-
- Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
-
- Public Const WH_KEYBOARD = 2
-
- Public hHook As Long
- Public Hooked As Boolean
- Public Key As String
-
-
- Public Function KeyboardProc(ByVal nCode As Long, _
- ByVal wParam As Long, _
- ByVal lParam As Long) As Long
- If wParam = 37 Or wParam = 39 Or wParam = 38 Or wParam = 40 Or wParam = 32 Then
- Hooked = True
- If wParam = 37 Then Key = "Left"
- If wParam = 39 Then Key = "Right"
- If wParam = 38 Then Key = "Up"
- If wParam = 40 Then Key = "Down"
- If wParam = 32 Then Key = "Space"
- End If
- KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
- End Function
-
- Sub Start()
-
- Dim i As Long
- Hooked = False
- hHook = SetWindowsHookEx(WH_KEYBOARD, _
- AddressOf KeyboardProc, _
- 0&, _
- GetCurrentThreadId)
- While (Not Hooked)
- i = i + 1
- Cells(1, 1) = i
- DoEvents
- Wend
- Call UnhookWindowsHookEx(hHook)
- MsgBox Key
- End Sub
Ответить
|
Номер ответа: 3 Автор ответа: GDK
Вопросов: 13 Ответов: 348
|
Профиль | | #3
|
Добавлено: 02.09.09 17:15
|
- Option Explicit
-
- Public Declare Function CallNextHookEx Lib "user32" _
- (ByVal hHook As Long, _
- ByVal nCode As Long, _
- ByVal wParam As Long, _
- ByVal lParam As Long) As Long
-
- Public Declare Function UnhookWindowsHookEx Lib "user32" _
- (ByVal hHook As Long) As Long
-
- Public 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
-
- Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
-
- Public Const WH_KEYBOARD = 2
-
- Public hHook As Long
- Public Hooked As Boolean
- Public Key As String
-
-
- Public Function KeyboardProc(ByVal nCode As Long, _
- ByVal wParam As Long, _
- ByVal lParam As Long) As Long
- If wParam = 37 Or wParam = 39 Or wParam = 38 Or wParam = 40 Or wParam = 32 Then
- Hooked = True
- If wParam = 37 Then Key = "Left"
- If wParam = 39 Then Key = "Right"
- If wParam = 38 Then Key = "Up"
- If wParam = 40 Then Key = "Down"
- If wParam = 32 Then Key = "Space"
- End If
- KeyboardProc = CallNextHookEx(hHook, nCode, wParam, lParam)
- End Function
-
- Sub Start()
-
- Dim i As Long
-
-
- Do
-
- Hooked = False
-
- hHook = SetWindowsHookEx(WH_KEYBOARD, _
- AddressOf KeyboardProc, _
- 0&, _
- GetCurrentThreadId)
-
-
-
- While (Not Hooked)
- i = i + 1
- Cells(1, 1) = i
- DoEvents
- Wend
-
-
- Call UnhookWindowsHookEx(hHook)
- MsgBox Key
-
- Loop
-
-
- End Sub
-
-
Глючит при нажатии пробела. Потому что фиг его знает. Влияние внеземных цивилизаций.
Не стал бы я этим пользоваться.
В любом случае.
Открой диспетчер задач и посмотри на загрузку процессора.
Ответить
|
Страница: 1 |
Поиск по форуму