KeyDown map to GUI form button downstate - vb6

Hi I would like to ask if it's possible to map the KeyDown for keyboards in Visual Basic 6 to turn the state of a graphical Command Button on the form to the "Down State" while keyboard key is pressed then back to raised when released? Thanks

I am aware of the problem here, because I did somewhat similar in the past and ended up by using an array of PictureBoxes instead of graphical CommandButtons.
Anyway, a simple workaround with CommandButtons is to keep the focus away by adding to the Form another control which can act as focus target. Remember: when a Form goes activated, it will place the focus to the first focusable control inside itself.
As You haven't specified in Your question what kind of keyboard state You need, below is a simple example with the a s d f keys. You will need less than 5 minutes to get it up and running.
Step 0:
Copy and paste following declarations to Your VB Form:
Option Explicit
Option Base 0
Const BM_SETSTATE = &HF3
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 Long
Dim Target(254) As Long
Step 1:
Add to Your Form an array of CommandButtons called, for instance, Button(0), Button(1), Button(2), Button(3) and so on.
Set the properties which You need (Picture, DownPicture, etc.) and set also:
TabStop: False
Double-click one of this CommandButtons. You can see, You have just one entry point for the whole array of Controls. Choose GotFocus from the event drop-down and put this piece of code:
Private Sub Button_GotFocus(Index As Integer)
PicFocus.SetFocus
End Sub
Step 2:
On Your VB Form, set this property:
KeyPreview: True
Double-click the Form, choose Load from the event drop-down and set Your desired mapping between a KeyCode and the corresponding CommandButton:
Private Sub Form_Load()
Target(65) = Button(0).hwnd ' 65: KeyCode for "a"
Target(83) = Button(1).hwnd ' 83: KeyCode for "s"
Target(68) = Button(2).hwnd ' 68: KeyCode for "d"
Target(70) = Button(3).hwnd ' 70: KeyCode for "f"
End Sub
Choose KeyDown and KeyUp from the event drop-down and put inside the two global keyboard event handlers this piece of code - respectively -1 for the down-state and 0 for the up-state:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' Debug.Print KeyCode
Call PostMessage(Target(KeyCode), BM_SETSTATE, -1&, 0&)
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Call PostMessage(Target(KeyCode), BM_SETSTATE, 0&, 0&)
End Sub
Step 3:
Lastly, add to the same VB Form the PictureBox mentioned above and set following properties:
Name: PicFocus
Appearance: 0-Flat
BorderStyle: 0-None
HasDC: False
TabIndex: 0
TabStop: False
Width: 255
Left: -1000
Press Ctrl+F5 and test if this is what You need.

The CommandButton control has mouse and keyboard down and up events:
Private Sub Command1_Click()
Debug.Print "click"
End Sub
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "keydown"
End Sub
Private Sub Command1_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "keyup"
End Sub
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "mousedown"
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "mouseup"
End Sub

Related

How Do I Turn On/Off X-Mouse in Tweak UI?

I'm attempting to create a VB6 executable (not sure of the proper syntax) that will toggle the X-Mouse option in Tweak UI under Windows 98SE. Ideally, I would like to have two scripts - one that turns it off (regardless of its state) and one that turns it on (again, regardless of its state).
I have been able to open the TweakUI control panel with the code below.
Private Sub Form_Load()
Call Shell("rundll32.exe shell32.dll,Control_RunDLL tweakui.cpl", vbNormalFocus)
End Sub
If possible, I would like it to do it without opening the TweakUI control panel.
As far as I can tell, changing the registry setting doesn't work as I would have to reboot the computer for that to take effect.
I have Registry Monitor 7.04 running. It captures the following:
Path: C:\WINDOWS\RUNDLL32.EXE
Command Line: "C:\WINDOWS\RUNDLL32.EXE" "C:\WINDOWS\SYSTEM\TWEAKUI.CPL", Tweak UI
Other: hKey: 0xC2A066F0
Honestly, I'm not sure how to move forward.
Not sure the best way to show progress on this, I'll just edit.
This code is very close.
Private Declare Function SystemParametersInfo Lib "user32" Alias _
"SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, _
ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Const SPI_SETACTIVEWINDOWTRACKING = 4097
'Click on this button to Activate XMouse
Private Sub Command1_Click()
SystemParametersInfo SPI_SETACTIVEWINDOWTRACKING, 0, True, 0
End Sub
'Click on this button to Deactivate XMouse
Private Sub Command2_Click()
SystemParametersInfo SPI_SETACTIVEWINDOWTRACKING, 0, False, 0
End Sub
Button 1 works correctly and Activates XMouse. But button two does not deactivate it.
SPI_SETACTIVEWINDOWTRACKING is the parameter that does this.
systemparametersinfo is the function call that gets or sets settings like this. See https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa
There is sample code using systemparametersinfo that changes the wallpaper. https://winsourcecode.blogspot.com/2019/06/changewallpaper.html
Thank you to all of the input. I was able to solve this problem.
Private Declare Function SystemParametersInfo Lib "user32" Alias _
"SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, _
ByVal lpvParam As Boolean, ByVal fuWinIni As Long) As Long
Const SPI_SETACTIVEWINDOWTRACKING = 4097
Private Sub Command1_Click()
retVal = SystemParametersInfo(SPI_SETACTIVEWINDOWTRACKING, 0, True, 0)
End Sub
Private Sub Command2_Click()
retVal = SystemParametersInfo(SPI_SETACTIVEWINDOWTRACKING, 0, False, 0)
End Sub
In addition to the help here, I stumbled upon a few gems that gave me what I needed.
Control the mouse speed under Windows 98 / 2000
and
Controling Active Window Tracking
A couple things of note. I had to include this or else nothing happened:
Const SPI_SETACTIVEWINDOWTRACKING = 4097
Also, the 3rd parameter was
ByRef lpvParam As Boolean
Instead of
ByVal lpvParam As Boolean
I was passing a pointer to a pointer instead of a pointer to a value

VB 6.0 remove scroll bar from listbox

Just wondering if anyone know's how to remove scroll bars from a listbox in VB 6.0? As I want to add a 'global' scroll bar for multiple listboxes. I have searched online, but all of the solutions require the code to be placed in the click event of the list box.
You can hide the scrollbars using the Windows API. Here's a sample project to get you started. Add a ListBox (List1) to a form and add the following code:
Private Declare Function ShowScrollBar Lib "user32" _
(ByVal hwnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
Private Const SB_VERT = 1
Private Sub HideVertScrollBar(LB As ListBox)
Call ShowScrollBar(LB.hwnd, SB_VERT, 0&)
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 25
List1.AddItem "Item " & i
Next
HideVertScrollBar List1
End Sub
Private Sub List1_Click()
HideVertScrollBar List1
End Sub
If you only call HideVertScrollBar in Form_Load, when you manually scroll (using the arrow keys), the scrollbar shows up again. We fix this by calling HideVertScrollBar in List1_Click as well.

TextBox Value Disappears after closing and reopening form

I have two forms: Form 1 and Form 2.
Form 1 has two Buttons and Form 2 has one textbox.
On Button 1 Click event I am writing "My Text" in my Form 2 TextBox and on button 2 I am showing Form 2.
What is happening is when I close my Form 2 using close [X] button and reopen it value in my Form 2 Textbox Disappears.
Please Help how can I resolve this
Form 1 Code:
Private Sub Command1_Click()
Form2.Text1.Text = "Parth"
End Sub
Private Sub Command2_Click()
Form2.Show
End Sub
Form 2 Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Me.Hide
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Cancel=true
Cancel=UnloadMode<>vbFormCode
Me.Hide
End Sub
Form1:
Option Explicit
Private Sub Command1_Click()
Form2.Text1.Text = "Parth"
End Sub
Private Sub Command2_Click()
Form2.Show vbModal, Me 'Since we are the owner, Form2 unloads when we do.
End Sub
If Form1 was not made the owner you'd need to explicitly unload Form2 within Form1's Unload event handler to avoid hanging the program.
You need to be careful not to test for the wrong UnloadMode value.
Form2:
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = True
Hide
End If
End Sub

procedure declaration does not match description of event or procedure having the same name

I am just novice and I tried to make a simple program in Visual Basic 6. The code is almost equivalent to that in the textbook. It was meant to be a kind of a paint program. Surprisingly, it couldn't be compiled with the error given in the title of this question.
This is the code:
Option Explicit
Dim Col As Long
Private Sub Form_Load()
AutoRedraw = True
BackColor = vbWhite
Col = vbBlack
DrawWidth = 3
End Sub
Private Sub Command1_Click()
CommonDialog1.ShowOpen
Form1.Picture = LoadPicture(CommonDialog1.FileName)
End Sub
Private Sub Command2_Click()
CommonDialog1.ShowSave
SavePicture Image, CommonDialog1.FileName
End Sub
Private Sub Command3_Click()
CommonDialog1.ShowColor
Col = CommonDialog1.Color
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
PSet (X, Y), Col
End Sub
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "Line1"
DrawWidth = 3
Case "Line2"
DrawWidth = 20
End Select
End Sub
The application crashes on the following line:
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
With the error:
procedure declaration does not match description of event or procedure
having the same name
The problem is here:
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Ok, since you are coding in VB6, you get to learn some of the tricks in the VB6 playbook. Temporarily rename the method to something else like qqToolbar_ButtonClick, then go to the designer and click the button in the toolbar to regenerate the event in the code.
In the event that the signature has been mistyped, it will regenerate from the designer correctly and you might see the issue.
Another check is to see if the ToolBar1 was added to a control array? In that case, the method signature needs to look like this:
Private Sub Toolbar1_ButtonClick(ByVal Index as Integer, ByVal Button As MSComctlLib.Button)
I hope one of these helps solve the issue for you.

Visual basic 6 events

How can i restrict an event to occur? Suppose i don't want textbox change event to occur when i press backspace.
Setting KeyAscii=0 in the KeyPress event will cause the keypress to be ignored.
Private Sub myTextBox_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyBack Then KeyAscii = 0
End Sub
Since the Change event doesn't pass you the code of the last key pressed, you'll have to store that in the KeyPress event, then you can immediately exit the Change event whenever the backspace key is pressed.
Private keyCode As Integer
Private Sub Text1_Change()
If (keyCode = vbKeyBack) Then
Exit Sub
Else
// do whatever it is you want to do in this event
// P.S.: I know this is the wrong comment syntax,
// but this code prettifier has a problem with
// VB6 comments
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
keyCode = KeyAscii
End Sub

Resources