Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Emulate pressing SpaceBar


Reply
 
Thread Tools Display Modes
  #1  
Old 06-05-2004, 08:15 AM
Antariksh's Avatar
Antariksh Antariksh is offline
Centurion
 
Join Date: Aug 2003
Location: Lothlorien
Posts: 167
Default Emulate pressing SpaceBar


My SpaceBar is not working.
I wanted to make a program so that when i press the ~ key,the spacebar is emulated.
How to doit?
__________________
Probable Impossibility is better than Impossible Probability
Reply With Quote
  #2  
Old 06-05-2004, 08:54 AM
Sebastian Mares's Avatar
Sebastian Mares Sebastian Mares is offline
Contributor
 
Join Date: Jun 2002
Location: Germany
Posts: 710
Default

Code:
SendKeys " "

You will also have to monitor the keys pressed using GetAsyncKeyState. Here is an example from API-Guide:

Code:
'In a module Public Const DT_CENTER = &H1 Public Const DT_WORDBREAK = &H10 Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As _ Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As _ Long, ByVal lpDrawTextParams As Any) As Long Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent _ As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, _ ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long _ Global Cnt As Long, sSave As String, sOld As String, Ret As String Dim Tel As Long Function GetPressedKey() As String For Cnt = 32 To 128 'Get the keystate of a specified key If GetAsyncKeyState(Cnt) <> 0 Then GetPressedKey = Chr$(Cnt) Exit For End If Next Cnt End Function Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) Ret = GetPressedKey If Ret <> sOld Then sOld = Ret sSave = sSave + sOld End If End Sub 'In a form Private Sub Form_Load() 'KPD-Team 1999 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email]KPDTeam@Allapi.net[/email] Me.Caption = "Key Spy" 'Create an API-timer SetTimer Me.hwnd, 0, 1, AddressOf TimerProc End Sub Private Sub Form_Paint() Dim R As RECT Const mStr = "Start this project, go to another application, type something, " & _ "switch back to this application and unload the form. If you unload the " & _ "form, a messagebox with all the typed keys will be shown." 'Clear the form Me.Cls 'API uses pixels Me.ScaleMode = vbPixels 'Set the rectangle's values SetRect R, 0, 0, Me.ScaleWidth, Me.ScaleHeight 'Draw the text on the form DrawTextEx Me.hDC, mStr, Len(mStr), R, DT_WORDBREAK Or DT_CENTER, ByVal 0& End Sub Private Sub Form_Resize() Form_Paint End Sub Private Sub Form_Unload(Cancel As Integer) 'Kill our API-timer KillTimer Me.hwnd, 0 'Show all the typed keys MsgBox sSave End Sub

Last edited by Deadalus; 06-05-2004 at 10:11 AM. Reason: Split the long code lines to prevent horizontal scrolling.
Reply With Quote
  #3  
Old 06-05-2004, 09:32 AM
Antariksh's Avatar
Antariksh Antariksh is offline
Centurion
 
Join Date: Aug 2003
Location: Lothlorien
Posts: 167
Default

Thanks, but i am not sure that will help
Now that the topic of SendKeys has arisen, how do I define WHERE to Send Key
__________________
Probable Impossibility is better than Impossible Probability
Reply With Quote
  #4  
Old 06-05-2004, 09:42 AM
Sebastian Mares's Avatar
Sebastian Mares Sebastian Mares is offline
Contributor
 
Join Date: Jun 2002
Location: Germany
Posts: 710
Default

If I understand corretctly, you want to emulate the space bar using the "*" key. You can monitor the state of "*" using GetAsyncKeyState and when you detected a key down event, use SendKeys " ". It will send the space bar to the focused window. Basically, it will act like pressing the space bar.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->