Manipulating KeyDown Keys

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I need to write an "easter egg" into our application.

It is designed to eliminate keystrokes, and it works great. This forces our application to use the hand held scanners that are plugged into the COM port. (If reading the COM port fails, we do not suppress the keystrokes)

However, occasionally someone in the IT department needs to remote in to do some diagnostics from another computer.

If KeyEventArgs.SuppressKeyPress = True, no one can operate the application remotely.

I'm looking for a good idea here.

I tried using the Alt key to signal that I want to bypass the Suppress feature, but then the Alt+(whatever was pressed) is unrecognizable by the system.

Example:
Say someone wants to enter their badge number: 123456 or log out of the system using the barcode text "LOGOUT". They should use the handheld scanners.

I want to enable keystrokes somehow, either if a special key is pressed (which seems like it would be easy to leak that information out) or some other technique.

Does any one know of a way to accomplish this task?
 
Ctrl+A sample ... and don't forget that KeyPreview should be set to TRUE

Code:
Private Sub Parser_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.Control And e.KeyCode = Keys.A Then
            ' ToDo: something
            e.Handled = False '/True
        End If
End Sub

Sorry if i have missunderstod your question.
 
Hi Kulrom and thanks for the reply.

This is the closest I seem to be able to get as well. The problem is, I want to collect the Key "A" and not the Key "Ctrl".

In other words, with the Control key pressed, all text entered should populate the textbox control and "look like" the Control key is *not* being pressed.
 
How about using the GetKeyState API? It will work even if your application is not focused.

Here's an example with a timer. I know it's not a good idea to use a timer, but then again this is just a simple example:
Code:
    Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Short

    Public Function IsKeyPressed(ByVal KeyCode As VirtualKeyStates) As Boolean
        Dim RetVal As Boolean = False

        Dim KeyStat As Integer = GetKeyState(KeyCode)
        If KeyStat < -1 Or KeyStat > 1 Then
            RetVal = True
        End If

        Return RetVal
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If IsKeyPressed(VirtualKeyStates.VK_MENU) Then
            Me.Text = "Alt pressed!"
        Else
            Me.Text = "Alt not down."
        End If
    End Sub

You will also need this enum:
Code:
Public Enum VirtualKeyStates
        VK_LBUTTON = &H1
        VK_RBUTTON = &H2
        VK_CANCEL = &H3
        VK_MBUTTON = &H4

        VK_XBUTTON1 = &H5
        VK_XBUTTON2 = &H6

        VK_BACK = &H8
        VK_TAB = &H9

        VK_CLEAR = &HC
        VK_RETURN = &HD

        VK_SHIFT = &H10
        VK_CONTROL = &H11
        VK_MENU = &H12
        VK_PAUSE = &H13
        VK_CAPITAL = &H14

        VK_KANA = &H15
        VK_HANGEUL = &H15   '/* old name - should be here for compatibility */
        VK_HANGUL = &H15
        VK_JUNJA = &H17
        VK_FINAL = &H18
        VK_HANJA = &H19
        VK_KANJI = &H19

        VK_ESCAPE = &H1B

        VK_CONVERT = &H1C
        VK_NONCONVERT = &H1D
        VK_ACCEPT = &H1E
        VK_MODECHANGE = &H1F

        VK_SPACE = &H20
        VK_PRIOR = &H21
        VK_NEXT = &H22
        VK_END = &H23
        VK_HOME = &H24
        VK_LEFT = &H25
        VK_UP = &H26
        VK_RIGHT = &H27
        VK_DOWN = &H28
        VK_SELECT = &H29
        VK_PRINT = &H2A
        VK_EXECUTE = &H2B
        VK_SNAPSHOT = &H2C
        VK_INSERT = &H2D
        VK_DELETE = &H2E
        VK_HELP = &H2F

        VK_LWIN = &H5B
        VK_RWIN = &H5C
        VK_APPS = &H5D

        VK_SLEEP = &H5F

        VK_NUMPAD0 = &H60
        VK_NUMPAD1 = &H61
        VK_NUMPAD2 = &H62
        VK_NUMPAD3 = &H63
        VK_NUMPAD4 = &H64
        VK_NUMPAD5 = &H65
        VK_NUMPAD6 = &H66
        VK_NUMPAD7 = &H67
        VK_NUMPAD8 = &H68
        VK_NUMPAD9 = &H69
        VK_MULTIPLY = &H6A
        VK_ADD = &H6B
        VK_SEPARATOR = &H6C
        VK_SUBTRACT = &H6D
        VK_DECIMAL = &H6E
        VK_DIVIDE = &H6F
        VK_F1 = &H70
        VK_F2 = &H71
        VK_F3 = &H72
        VK_F4 = &H73
        VK_F5 = &H74
        VK_F6 = &H75
        VK_F7 = &H76
        VK_F8 = &H77
        VK_F9 = &H78
        VK_F10 = &H79
        VK_F11 = &H7A
        VK_F12 = &H7B
        VK_F13 = &H7C
        VK_F14 = &H7D
        VK_F15 = &H7E
        VK_F16 = &H7F
        VK_F17 = &H80
        VK_F18 = &H81
        VK_F19 = &H82
        VK_F20 = &H83
        VK_F21 = &H84
        VK_F22 = &H85
        VK_F23 = &H86
        VK_F24 = &H87

        VK_NUMLOCK = &H90
        VK_SCROLL = &H91

        VK_OEM_NEC_EQUAL = &H92    ' '=' key on numpad

        VK_OEM_FJ_JISHO = &H92    ' 'Dictionary' key
        VK_OEM_FJ_MASSHOU = &H93    ' 'Unregister word' key
        VK_OEM_FJ_TOUROKU = &H94    ' 'Register word' key
        VK_OEM_FJ_LOYA = &H95    ' 'Left OYAYUBI' key
        VK_OEM_FJ_ROYA = &H96    ' 'Right OYAYUBI' key

        VK_LSHIFT = &HA0
        VK_RSHIFT = &HA1
        VK_LCONTROL = &HA2
        VK_RCONTROL = &HA3
        VK_LMENU = &HA4
        VK_RMENU = &HA5

        VK_BROWSER_BACK = &HA6
        VK_BROWSER_FORWARD = &HA7
        VK_BROWSER_REFRESH = &HA8
        VK_BROWSER_STOP = &HA9
        VK_BROWSER_SEARCH = &HAA
        VK_BROWSER_FAVORITES = &HAB
        VK_BROWSER_HOME = &HAC

        VK_VOLUME_MUTE = &HAD
        VK_VOLUME_DOWN = &HAE
        VK_VOLUME_UP = &HAF
        VK_MEDIA_NEXT_TRACK = &HB0
        VK_MEDIA_PREV_TRACK = &HB1
        VK_MEDIA_STOP = &HB2
        VK_MEDIA_PLAY_PAUSE = &HB3
        VK_LAUNCH_MAIL = &HB4
        VK_LAUNCH_MEDIA_SELECT = &HB5
        VK_LAUNCH_APP1 = &HB6
        VK_LAUNCH_APP2 = &HB7

        VK_OEM_1 = &HBA    ' ';:' for US
        VK_OEM_PLUS = &HBB    ' '+' any country
        VK_OEM_COMMA = &HBC    ' ' ' any country
        VK_OEM_MINUS = &HBD    ' '-' any country
        VK_OEM_PERIOD = &HBE    ' '.' any country
        VK_OEM_2 = &HBF    ' '/?' for US
        VK_OEM_3 = &HC0    ' '`~' for US

        VK_OEM_4 = &HDB   '  '[{' for US
        VK_OEM_5 = &HDC   '  '\|' for US
        VK_OEM_6 = &HDD   '  ']}' for US
        VK_OEM_7 = &HDE   '  ''"' for US
        VK_OEM_8 = &HDF

        VK_OEM_AX = &HE1   '  'AX' key on Japanese AX kbd
        VK_OEM_102 = &HE2   '  "<>" or "\|" on RT 102-key kbd.
        VK_ICO_HELP = &HE3   '  Help key on ICO
        VK_ICO_00 = &HE4   '  00 key on ICO

        VK_PROCESSKEY = &HE5

        VK_ICO_CLEAR = &HE6

        VK_PACKET = &HE7

        VK_OEM_RESET = &HE9
        VK_OEM_JUMP = &HEA
        VK_OEM_PA1 = &HEB
        VK_OEM_PA2 = &HEC
        VK_OEM_PA3 = &HED
        VK_OEM_WSCTRL = &HEE
        VK_OEM_CUSEL = &HEF
        VK_OEM_ATTN = &HF0
        VK_OEM_FINISH = &HF1
        VK_OEM_COPY = &HF2
        VK_OEM_AUTO = &HF3
        VK_OEM_ENLW = &HF4
        VK_OEM_BACKTAB = &HF5

        VK_ATTN = &HF6
        VK_CRSEL = &HF7
        VK_EXSEL = &HF8
        VK_EREOF = &HF9
        VK_PLAY = &HFA
        VK_ZOOM = &HFB
        VK_NONAME = &HFC
        VK_PA1 = &HFD
        VK_OEM_CLEAR = &HFE

    End Enum
 
Another idea would be to make the program a one instance application. Then you could run the program with some custom commandline arguments and catch that in the first instance.
 
Back
Top