Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Trapping Keystrokes In VB6

How do you trap something like 'Ctrl' + a letter or number?


View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Trapping All Keystrokes
Could someone please write a demo project that uses API to track EVERY keystroke, regardless of which app has the focus?

Arien Talabac, author of Tiny Clock, the skinnable alarm clock for Windows--check it out at

Getting Keystrokes?
here is my thing.. i want that when you write in a textbox it appears like you are wrtiting another thing but the actual text you are inputing is saving ina varible..

you see:
Hello my name is
and you are actually typing:

i used to do this in Qbasic by usnig inkey$ i guess :P

many thanks

Get The Keystrokes

how can i get all the keypressed when my app is active ?

my app contain a single form with many objects..

itz hard to do the keypress event code for all the objects..

please help


i would like to make a program that get all key entered and put them in a file.
how can i do that?

How can i detect keystrokes even if the form is hidden.

Is it possible to write a program that simulates keystrokes on a keyboard? I found this class module, but I dont know how to activate it by clicking on a button on a form. I just started with Visual Basic, so I dont know much. I hope these forums help me learn.

I was thinking about creating a program which would press the key Ctrl in a certain window. I tryed using sendkeys and WM_KEYDOWN and it doesn't work quite that well. Is there any other way of coding it? Like apis? Thanks.

I need to record every Key Stroke that is typed when this program is running and save it into a text file,
i know how to save it to a text, but its the recording keystroke part, something to do with ASCII
and how would i save these keys or would i put it write into the text file.


Hey, im making a keylogger program, an i wanna know when somoene is " typing " on your computer, how it will log them onto the txt file. This is waht i have on the code already.

Private Declare Function GetAsyncKeyState Lib "User32" _
(ByVal vKey As KeyCodeConstants) As Long

Using Keystrokes....
OK, I need a little help... I was wondering how to capture keystrokes. I'm making a little game here and was wonding how to use the arrow keys as controls.

I can build the logic behind this, but would just like a form to start with. When the form is up and you press either up, down, left or right it would return a message box.

I can do the rest...

Thanks for any help you can offer...



how can i get my app to pick up keystrokes (and save em) from anywherein windows.....

thanx in advance.

I made a program that would make a keystroke every 3 secs. But when I used it in a game, it didn't do anything. But, it did. For example if I wanted "3" to move, and I had that has the keystroke, I wouldn't move at all, but if I had the textbox open to chat to other players, it would be typing 333333... So, its working, but it only seems to be working as a string, not like the real key being pressed. Can anyone help me?

Function I am using is SendKeys
SendKeys "3"


I have a vb screen which needs to be unloaded after 5min, if there are no activites done on the screen ex: if there r no keystrokes or mouse clicks .
Can someone write me how to go about this.

Thanks a bunch!

Is there a visualbasic code to record all keystrokes that are typed, while the visual basic program is running on the background?

Ben Reynders

Hi list,

I'm delving into automation using VB...I am hoping to develop a simple automate procedure to use on MapInfo...this is my first keystroke code that I managed to find on the net...when I run it, and press the button nothing just sits there, it should close the window down...
any and all help would be appreciated
here's the code I'm using.

Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub Command1_Click()


keybd_event VK_MENU, 0, 0, 0 ' press Alt
keybd_event VK_SPACE, 0, 0, 0 ' press Space
keybd_event VK_SPACE, 0, KEYEVENTF_KEYUP, 0 ' release Space
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0 ' release Alt

'Type the N key.
keybd_event VK_N, 0, 0, 0 ' press N
keybd_event VK_N, 0, KEYEVENTF_KEYUP, 0 ' release N
End Sub


How do i record a keystroke in a textbox?

How Do I Suppress Keystrokes???
I have a REALLY annoying problem with my current project - I am using Direct Input in a DD7 game using VB6.

Every time I hit a key while the game is running, the keystroke is implemented in the IDE, so that when I quit the game and get back to the code, I have "qpaoqoaqpaopoqpqpaoqpoa" written in the actual code. I copied the new code I have directly from another of my projects (which does NOT have this problem). I have changed all the form's properties so that they are identical in both projects, but the problem still persists. I've been trying to solve this problem for the last 3 hours, the relevant code is totally identical because I copied it, I can't understand why it's doing this! GOD I AM SO MAD!!

EDIT: PLEASE PLEASE PLEASE someone help me! It's been two days now, there MUST be a way of fixing this! Has anyone else had this problem?? Can anyone help me??

Sending Keystrokes
i am sending a text to an html form that contains java scripts. this form is displayed on a browser,but does not have a URL. it only has a title on its title bar.for example "flexcube - Internet Retail Banking".and i think i will reference the page with the title.users need to log in before the page will be displayed.the page contains some textboxes. please i need to send a text from my visual basic textbox to this form textbox. please this is on my neck urgently.


Capture Keystrokes In VB
Is it possible to capture these keys in VB?
Windows key + z = Msgbox "Windows key + z"
Alt + Shift + s = Msgbox "Alt + Shift + s"
Ctrl + Shift + a = Msgbox "Ctrl + Shift + a"

If yes, how?

Simulating Keystrokes
I'm trying to simulate keystrokes for any application in focus Using a hotkey. I have it working for the <space> key, but it won't seem to work for the quote key ("). Maybe someone can tell me what I'm doing wrong?

Const VK_Space = 32
Const VK_Quote = 34
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub VBHotKey1_HotkeyPressed()
keybd_event VK_Space, 0, 0, 0
keybd_event VK_Space, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_Quote, 0, 0, 0
keybd_event VK_Quote, 0, KEYEVENTF_KEYUP, 0
End Sub

I'm using a hotkey, and I know the hotkey is working, because the space works, but the quote will not.

Any help would be much appreciated.

Sending Keystrokes
I have a handle to a certain window (a form) can i send a keystroke ALT+R like this?

SendMessage THWnd, WM_KEYDOWN, VK_R, 536870912

If yes why doesn't it work and if no tell me how i can accomplish this.

This should put a tick in a CheckBox on the form, should i Perhaps use the handle of the checkbox' window instead?

Thanks for the help.

Detecting Keystrokes
is there any way i can detect keystrokes or even check what has been pressed even outside the form. in this case, i want to detect if keycodes 178 or 179 have been pressed.


Capturing Keystrokes
I have this VB6 app with some controls on a form.

I want to interecept each keystroke before any control on the form sees the keystroke.

The form.keypreview=true is NOT working. I don't know why. I suspect it is caused by a 3rd party control I'm using.

Is there another way to capture the keystroke other than using (formname).preview=true? I might have to use the Windows API but I thought I'd try here first.

Timed KeyStrokes?
How would i be able to tell how long a button has been pressed for? Eg:

If Spacebar was pressed for 3 seconds then
elseif Spacebar was pressed for 4 seconds then

Restricting Keystrokes
I'm working on restricting keystrokes on some combo and text boxes. I've been using the keypress event and I easily figured out how to capture the keystrokes and change them to a different keystroke, but I can't figure out how to just ignore the keystroke Ex: They type "*" I don't want anything to appear in the textbox. I can achieve this if I put up a message box or print the keystroke to the immediate window but I really don't want to do that. Are there any other alternatives?

Intercepting Keystrokes
I was wondering if there was a way to make intercepted keystrokes work system wide. Example: Change F10 to ESC, have my app running in the background and have it affect all programs running on the system. I know how to intercept keystrokes for my app, it's the having it affect other programs part I don't know.


"If you would have been listening, you would have heard what I meant to say." -Rod McKuen

Recording Keystrokes
I need help recording keystrokes from other programs. I want to be able to save what's been typed from other programs, not just mine. So basically, anything typed in the keyboard is what I would want to be saved. NE1 who can help, please do! I'm really stumped.

Simulating Keystrokes In VB6
I am trying to make a program, and I need some code that will simulate pressing the enter key, to control another program. Thanks!

Capturing Keystrokes
I have a big problem...

I have VB 5.0 and I wanna do a simple program, with just one form, to capture the keystroke of the space bar and press it twice...

Like, i press once the space bar, and it presses twice.

Thanx a lot.

Repeating Keystrokes
I have a image control which receives a key stroke. I press and hold an arrow key. But I only get one key stroke. I would like it to generate multiple keystrokes. Any suggestions how to make this happen?

Send Keystrokes
How do I send keystrokes to another application from a vb application?

Sending Keystrokes
how would you go about sending virtual keystrokes to an application? SendInput? if so how do you use it? Ive read lots of examples but theyre too complicated for me and use too much at once for me to figure it out, which is why Id like to start off with a single letter so it doesnt overwhelm me with confusion. Ive got a thread but noones really been able to help me out with it and I think your project is very similar to mine, but the diffrence is, Id like mine to send virtual keystrokes to a single application of which I would know the handle of, I just dont know how to use the API to do it. From what I know I'm assuming it will be something like:

const thevirtualkey
private declare sendinputapi

sendinput handle(?), virtualkey(?), ?

Edit: Create your own threads - don't hijack others

Virtual Keystrokes To IE5/6
Hey Folks!
While most seem to be trying to "Capture" keystrokes for various reasons, I actually need to "trick" Internet Explorer into thinking some have been "typed". I run a retail business and have a POS system that handles my purchase orders, but one of my main wholsalers is working hard to encourage "OnLine Ordering". This means that I now have to manually key my weekly orders. This process is not only time consuming, but very prone to operator error. I have the capability to output the item/quantity list from my POS Application in any text format, and the wholesaler's "Quick Entry Web Page" consists of 40 fields (20 Item Number, 20 Quantity) that are Tab or Chr(13) if I could give the Web Page "Focus" and then "Output Keystrokes" to it, I could enter up to twenty Items at a time without human error. The four possible solutions I can see to this are as follows (in increasing levels of complexity):

1) A software solution that uses the API or some other technique to "output" keystrokes from a file or other data source.

2) A "null modem" that sets a comport up as a "KeyBoard" and the characters "output" are then routed directly back into the "input".

3) A "Keyboard wedge" similar to one used by a barcode reader that uses a series of calls to the keyboard from the PC to initiate a micro-controller to "Begin" to echo characters to the PC until it recieves a "terminate" string.

4) Create a USB keyboard interface to act as an auxilary keyboard similar in effect to solution 3.

Solution 3 and 4 require a great deal of time and effort and I view them only as absolute last choice options. If anyone has any ideas or code suggestions for solution 1 or 2, please share them with me. I am quite certain that there must be an API call that can both "Hook" a keyboard key sequence (like <CTRL> <ALT> <~>) and then replace that sequence with a virtual series of keystrokes, I just can't find any info on how to do it. Perhaps there is an even esier way than those mentioned, PLEASE feel free to point these out to me.

Thanks for taking the time to read this:-)

Sending Keystrokes To Another App
How to send keystrokes to another application using Win32Api (not using SendKeys function)


WM_ Messages Associated With Keystrokes
hi, I'm subclassing a window, and I need to block _all_ of the keystrokes it recieves, it seems to be getting ctrl/alt/shift I guess some system keys even though i'm blocking wm_char/keydown/keyup, and the wm_syschar/keydown/keyup as well. Is there a list somewhere of the wm_ messages associated in any way with keystrokes?
please help, tnx

Sending Keystrokes And Etc
i am making a program as an accessory for runescape where there is a webbrowser1 control and want to make the program which you can type your message in a textbox and select how many times to type it and it will type it into the webpage(javaapplet). Do you need the specific x, y coords relative to the form or wat? Plz help me by showing how to do it!

Disable KeyStrokes
Is there a way to diable Ctrl-alt delete and alt tab?

Preventing Keystrokes
Allright, Im programming a MMORPG, I dont want Numbers Nor Spaces in the Name. So, I set this up;

Private Sub txtName_KeyPress(KeyAscii As Integer)
Dim ch As String

ch = Chr$(KeyAscii)
If Not ( _
(ch >= "a" And ch <= "z") Or _
(ch >= "A" And ch <= "Z") _
) Then
' Cancel the character.
KeyAscii = 0
End If
End Sub

Works exactly how I wanted,But Wait, I can't backspace now. So, Does anyone know how I could make it allow the backspace key?

Reading Keystrokes

I have a question regarding reading keystrokes when the program is minimized and is not in focus... For example the user has word open and types something. Is there a way to record their typing. It should work similar to Keydown. I am thinking a way using some API it should be possible. Can anyone give me an example?...

Blocking Keystrokes
Hey, I posted this before, but the thread has been lost.
I've got this code to 'hotkey' one of the buttons on my keyboard.
It's originally designed to load up the calculator, and I'm trying to change that.
My code is:

VB Code:
'**************************************'Windows API/Global Declarations for :Di'     sable Low Level Keys'**************************************Option Explicit Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As LongPublic Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongPublic Const WH_KEYBOARD_LL = 13 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As IntegerPrivate Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long Private Const WM_KEYDOWN = &H100Private Const WM_LBUTTONDOWN = &H201Private Const WM_LBUTTONUP = &H202Private Const BM_SETCHECK = &HF1Private Const BM_GETCHECK = &HF0Private Const BM_CLICK As Long = &HF5 Private Type KBDLLHOOKSTRUCT    vkCode As Long    scanCode As Long    flags As Long    time As Long    dwExtraInfo As LongEnd TypeDim p As KBDLLHOOKSTRUCT Public Function LowLevelKeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As LongDim fEatKeystroke As BooleanIf nCode = 0 Then    CopyMemory p, ByVal lParam, Len(p)    fEatKeystroke = (p.vkCode = 183 Or p.vkCode = 172)End If If fEatKeystroke Then    LowLevelKeyboardProc = -1    If wParam = WM_KEYDOWN Then        Dim hWnd As Long, origState As Long        hWnd = FindWindow("#32770", "Ventrilo")        hWnd = FindWindowEx(hWnd, 0&, "Button", IIf(p.vkCode = 183, "Mute Microphone/Binds", "&Mute Sound"))        If SendMessage(hWnd, BM_GETCHECK, 0&, 0&) = 0 Then            SendMessage hWnd, BM_SETCHECK, 1, 0&            origState = 1        Else            SendMessage hWnd, BM_SETCHECK, 0, 0&            origState = 0        End If        Beep 250 * (4 - origState) - 45.45 * (183 - p.vkCode), 200    End IfElse    If p.vkCode = 180 Then        LowLevelKeyboardProc = -1        Unload frmMain    Else        LowLevelKeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)    End IfEnd If End Function

I've compiled this, and put it in the StartUp folder, and it runs windowless and not showing in the taskbar. It works perfectly, except this:
Every time the computer is rebooted, the first time the button is pressed, the code will run, and the SendMessage is sent, the checkbox is ticked, however - the calculator is also run, and the default message isn't being blocked. I can't recreate this problem in the IDE, and it only happens on the very first click.

Anyone have any ideas on what could be causing this?

Capture Keystrokes
Hello Ppl All Over The World Reading This Post.

I have a problem here i used to monitore my son what he was doing online but
I cant use keyloggers on our pc anymore because they are usally detected by antivirus and then my son would find out im watching him so since i just learned a bit of programming in vb i tried to make one myself W/O results because i have no idea how to start on this so any help is highly appreiciated

Greetz Tim

Sensing Keystrokes
I need a routine that will allow me to sense the keyboard and keystokes. I have tryed to use the Form_KeyPress and FormKeyDown routines with only partial success. After a while they seems to lock up and stop functioning.

Is there another routine that uses the API or something, that will allow me to see keystrokes. FYI, I have a keyboard that has a Shuttle/Jog function on it. It uses a driver that when you activate either the Shuttle or Jog, it sends keystrokes. I need to capture these keystrokes.

Capturing Keystrokes
I need to capture each letter typed by the user
How can i do that?

after the user has finished typing, i need to send all the typed characters into an array
and this??

Thanks in advance

Grabbing Keystrokes
I have a program that counts mouse clicks and keyboard hits...

one issue if someone holds down the key it still counts as hitting it lots of time.. how can I make it do just keyup ?

I'm calling getketstate for every key valued from 0 -144.. should be mostly normal keyboard keys..

another question since I am running through these as binary call not the hex ones, how do I know what the values are, the virtual key list in MSDN is by hex not decimal...

here's my grabber code

Dim i As Integer
For i = 3 To 141
If (GetAsyncKeyState(i) <> 0) Then
Text1.Text = Text1.Text & Str(i) & ","
Debug.Print i
KeyboardArray(i) = KeyboardArray(i) + 1
' if we got here then a new key was hit.. add'em up
HitCount = HitCount + 1

End If

Detecting Keystrokes

How can I make it so my VB App detects a certain keystroke even when your not in it?

Say you were in a game and u pressed a certain set of keys, how could my VB App detect that and respond to it?

Thanks, I have this code:

VB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)If Shift = vbCtrlMask + vbShiftMask And KeyCode = vbKeyQ Then'Anything i wana do hereEnd IfEnd Sub

With the forms KeyPreview = True, it doesnt detect the keystroke when the form hasnt got the focus.

Thanks for the help, this is getting pretty hard now.

Stopping Keystrokes
Hi,im a fairly new vb programmer and i was curious....
is there a way to actualy STOP people from using keystrokes like alt-f4,ctrl-alt-delete,[windows key] etc....

any response would be appreceated

Send Keystrokes
This is just like what GameCommander does. It sends keystrokes to an application, for example, t H e l l o enter. This sends a chat message in NASCAR 2002. It hooks on to the process. I am wondering, what is the API call to send key commands? I know how to hook into a window ETC, just need to know how to send the commands.


Mixed Keystrokes
I am making a text editor and a RTB servers as the main interface. I have a form with key preview turned on. When someone types in the RTB the form doesn't get the keystrokes at all? How can I let the form get the keystrokes first, deal with them and then let the RTB have them?

Sending Keystrokes To Another App

Simple question..

How do i send a keystroke ( F12 for example ) to another program that is running by using its hwnd?


Copyright 2005-08, All rights reserved