?
Solved

Need Some additional help with KeyBoard Hooking Functionality.

Posted on 2005-03-22
10
Medium Priority
?
516 Views
Last Modified: 2009-07-29
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
Comment
Question by:rrbecker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 23

Accepted Solution

by:
gecko_au2003 earned 750 total points
ID: 13607442
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13607444
CCHAR(Hookstruct.vkCode)
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13607447
I do not have vb.net installed so I can not test any coding out just so you are aware.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:rrbecker
ID: 13612834
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13613861
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
 
LVL 2

Author Comment

by:rrbecker
ID: 13614680
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13614716
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
 
LVL 2

Author Comment

by:rrbecker
ID: 13614973
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
 
LVL 2

Author Comment

by:rrbecker
ID: 13617211
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 13617821
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question