Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

Need Some additional help with KeyBoard Hooking Functionality.

Greetings All,

I have been working on a utility to Trap and Record ALL KeyStrokes in VB.NET. I have received some from EE member gecko_au2003 and from browsing the EE knowledge base.

However, since I am not as versed in VB.NET as maybe I should be I am having some additional trials. I am using the example code that is available at this location:

http://www.developer.com/net/net/article.php/11087_2193301_1, and have it running enough to know that I need help in resolving a couple of issues.

At this point in my development I really only have one major questions:

Q: When the code runs, and I have type some Chars in from the keyboard, and  look at them in 'Debug' mode, I find that all of the chars are output and displayed in 'UpperCase' chars. So my question is 'How do I retrieve and display the char just as I have typed it, ie 'A or a'.

I have tried to use 'MapVirtualKey' for both the 'Virtual KeyCode' and the 'ScanCode' but with no luck.

Any help or insite would be much appreciated.

----------------start code snippet----------------------------
' Implement this function to block as many
    ' key combinations as you'd like
    Public Function IsHooked( _
      ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean

        Debug.WriteLine("Hookstruct.vkCode: " & Chr(Hookstruct.vkCode))
        'Debug.WriteLine("Hookstruct.scancode: " & Chr(MapVirtualKey(Hookstruct.vkCode, 2)))
        Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
        Debug.WriteLine(Hookstruct.vkCode = VK_ESCAPE)
        Debug.WriteLine(Hookstruct.vkCode = VK_TAB)

        'SW.WriteLine(Chr(Hookstruct.vkCode))


        'If (Hookstruct.vkCode = VK_ESCAPE) And _
        '  CBool(GetAsyncKeyState(VK_CONTROL) _
        '  And &H8000) Then

        'Call HookedState("Ctrl + Esc blocked")
        'Return True
        'End If

        'If (Hookstruct.vkCode = VK_TAB) And _
        '  CBool(Hookstruct.flags And _
        '  LLKHF_ALTDOWN) Then

        'Call HookedState("Alt + Tab blockd")
        'Return True
        'End If

        'If (Hookstruct.vkCode = VK_ESCAPE) And _
        '  CBool(Hookstruct.flags And _
        '    LLKHF_ALTDOWN) Then

        'Call HookedState("Alt + Escape blocked")
        'Return True
        'End If

        Return False
    End Function
--------------end code snippet--------------------------

Please note that I have commented out the code that 'Blocks' certain key sequences. I will address my needs for something of this nature after I get the 'Regular Chars' to function in the manner described. (BTW I can provide my entire listing if desired.

Should I be using 'GetAsyncKeyState' to retrieve the 'Shifted and Non-Shifted' chars and if so how?

Thanks in advance,

rrbecker

PS If necessay I will increase the points to cover multiple participents.
0
rrbecker
Asked:
rrbecker
  • 6
  • 4
1 Solution
 
gecko_au2003Commented:
I found this :

Trapping Key Combinations
Determining if specific key combinations are being pressed requires some tricky gyrations. (Keep in mind that we are working at a pretty low level here.) This code remains pretty much unchanged from the November article. The basic idea is to read the current key press in the KBDLLHOOKSTRUCT.vkCode. If you need to look for specific multi-key combinations, you may need to call GetAsyncKeyState to determine whether additional keys are being held. For example, we call GetAsynckeyState(VK_CONTROL) in listing 2 to see whether the Ctrl key is being held down.

So looks like the GetAsyncKeyState is to determine when a key combination is pressed.

I just thought when you declare the function like so :

Public Delegate Function KeyboardHookDelegate( _
  ByVal Code As Integer, _
  ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
               As Integer

There is Code, Is code the equivalent to KeyAscii in Visual Basic ? If so you could use this along with a function in vb.net to convert the KeyAscii code back into its actual Characther, ie if you pressed a you would get 97 if you pressed A you would get 65. I think in visual basic you use the Chr Function in visual basic to convert them back ie if you know that Code is equal to keyascii you could do something like so

Chr(Code)

and that should return the actual character.

I found this site :

http://www.startvbdotnet.com/language/conversion.aspx

It says to use CCHAR to convert to CHAR datatype so maybe try CCHAR(Code) and assign that to a textbox text value and see if that gives you the desired result :)
0
 
gecko_au2003Commented:
CCHAR(Hookstruct.vkCode)
0
 
gecko_au2003Commented:
I do not have vb.net installed so I can not test any coding out just so you are aware.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rrbeckerAuthor Commented:
Hi gecko_au2003

Thanks for the response, however, you are not providing any New information. All of 'This' information is provided in the links that you provided before and I have gone over it and it doesn't provide any insite into my problem.

I too have scoured the Internet (and EE) and found 'things' that I thought would be of some use. One of the things that I found was a reference to 'MapVirtualKey'  which suposedly can be used to map 'ScanCodes' as well as 'VirtualKeys'. However, I must be using the function wrong, or the KBDLLHOOKSTRUCT structure doesn't contain what I think it cointains.... a ScanCode ie Hookstruct.scanCode. (see exerp below)

".....Debug.WriteLine("Hookstruct.vkCode: " & Chr(Hookstruct.vkCode))
        'Debug.WriteLine("Hookstruct.scancode: " & Chr(MapVirtualKey(Hookstruct.vkCode, 2)))...."

As far as Converting the char with CCHAR() or Chr() this will only provide the 'printable' version of what is contained in 'Hookstruct.vkCode' (see above). The core of my problem is what is stored in the KBDLLHOOKSTRUCT  structure. It appears that this structure does not contain a 'true' representation of a common alpha char ie. a '97 for lowercase a' or a '65 for a Uppercase A', it only contains the Uppercase for each char.. (65 for A).

So... my problem lies elsewhere.

Thanks for the help, I do have an idea that I am going the try out today and if I reach some level of success then I will let you know.

In the mean time, I am alwasy open to new ideas and suggestions.

Thanks,

rrbecker
0
 
gecko_au2003Commented:
With the storing of the KeyAscii code or w/e it is called in vb.net, does it have some way of working out the difference between the KeyAscii code for the Capital A and the lower case A , and so on for other letters, or keys pressed ?

IE :

'97 for lowercase a' or a '65 for a Uppercase A',

The difference between 97 and 65 is 32 so is there a common difference or some math you could use to calculate the difference to allow you to get the lowercase value ?

If that is not what you are trying and that is useless then what is your idea that you are going to try out, if you do not mind me asking.
0
 
rrbeckerAuthor Commented:
Hi gecko_au2003,

Ok, I've got it worked out.

You seem to be missing the root problem here. As Mentioned ealier ALL Key Stroke data is placed into the KBDLLHOOKSTRUCT structure whenever a Keyboard event takes place, wheither it is a 'KeyDown' or 'keyUp' event'. The structure is defined as the following:

---------------structure definition--------------
Public Structure KBDLLHOOKSTRUCT
        Public vkCode As Integer
        Public scanCode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure
--------------------------------------------------

The field of interest is the 'vkCode' which is the 'Virtual-Key Code'. By definition (as it turns out) ALL 'Virtual-Alpha-Chars' are stored as their Uppercase equivilent, ie A B C D......

The 'CLUE' to the final resolution is wheither or not the 'desired' char is 'SHIFT'ed' or Not...

If the desired char is suposed to be an Uppercase 'A' then we need to test to see if the 'Shift' key is being 'HeldDown'. If it is then we can 'output' the capital Letter 'A' directly from 'vkCode'. (example:SW.WriteLine(Chr(Hookstruct.vkCode))

If not held down then we can output the lowercase 'a' by converting the char to lowercase.(example:SW.WriteLine(Chr(Hookstruct.vkCode.ToString.ToLower))

BUT, to do this we Have to Test the 'Shift Key'. (see example below)

--------------partial solution snippet-------------------------------------------------
Select Case Hookstruct.vkCode
            Case 65 'A
                If CBool(GetAsyncKeyState(VK_SHIFT)) Then
                    SW.WriteLine(Chr(Hookstruct.vkCode))
                Else
                    SW.WriteLine(Chr(Hookstruct.vkCode.ToString.ToLower))
                End If
                Return False
End Select
----------------------------------------------------------------------------------------------

Be Aware that this is only for 'Shifted' chars, there is still the matter of  'Control and Alt' sequences that need to be addressed as well as the !@#$%^&*() chars (ie shifted numerics.1234567890....)

So... I have an idea of how to proceed. While this may be a 'Brute Strength' method, for the moment, it is all that I have. If there is a more elegant solution then I would be happy to review it and implement it.

I am going to leave this question open for a  day or so to see if anyone has any additional information or a different approach.

Thanks for your help,

rrbecker
0
 
gecko_au2003Commented:
Your welcome, Now I understand exactly what you mean. At the end of the day as long as it works, that is the main thing :) I cant understand why people have not input anything with regards to this question especially considering that I am not really a vb.net programmer yet. I will get there but it will take me some time :) Is there any chance I can get a proto type copy of this program once you have finished with it ? If that is ok ?
0
 
rrbeckerAuthor Commented:
Hi All,

Slight change to the code snippet:

-------------snippet---------------------
Select Case Hookstruct.vkCode
            Case 65 To 90 'A-Z
                If CBool(GetAsyncKeyState(VK_SHIFT)) Then
                    SW.WriteLine(Chr(Hookstruct.vkCode))
                Else
                    Dim MyChar As String
                    MyChar = Chr(Hookstruct.vkCode)
                    SW.WriteLine(MyChar.ToLower)
                End If
                Return False
End Select
-------------------------------------------------

rrbecker
0
 
rrbeckerAuthor Commented:
Hi gecko_au2003,

I think I can move forward on this project with the knowledge that I currently have. So I guess I'll close this thread.

I am reluctant to post a working prototype of this program but if you do a 'Google' search for 'Macro Builders' you will find many offerings of programs that do similar things. Some, I came across, even offer source code. These programs use a 'Journaling' approach which you might find useful. (look for JournalPlaybackProc and JournalRecordProc on Google and within VisualStudio - VB6).

My progrm is a Greatly reduced SubSet of what is offered with Journaling and is being taylored for my needs.

So Thanks again

rrbeker
0
 
gecko_au2003Commented:
Your welcome, thank you for grade and points and that is understandable with regards to the not post a working prototype of this app. I just meant emailing me it, at my email address which is in my profile :) But it is up to you :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now