Visual Studio 2008 Mouse and Key Down on out of form - vb.net

In Visual Studio 2008, how do I implement this code?
If MouseButtons() = Windows.Forms.MouseButtons.Left Then
SendKeys.Send("{3}")
SendKeys.SendWait("{1}")
End If
When I press the left button of the mouse, I want it to send 3 & 1 to my Game.
I'll be using this code for a game.

You can get the mouse click with this.
Add a timer "for my code call it DeskClick"
Add a textbox called ShowMouseClick "so you can check"
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Private Const LBUTTON = &H1
Private Const RBUTTON = &H2
Private Sub DeskClick_Tick(sender As Object, e As EventArgs) Handles DeskClick.Tick
If GetAsyncKeyState(LBUTTON) Then
ShowMouseClick.Text = "Left Click"
ElseIf GetAsyncKeyState(RBUTTON) Then
ShowMouseClick.Text = "Right Click"
Else
ShowMouseClick.Text = ""
End If
End Sub
you also need to turn of PInvokeStackImbalance
Go to Debug then select Exceptions Then MDA and deselect PInvokeStackImbalance
Maybe this is not the best way but it works perfect in my app Whit out any problem
To capture the event and do something
Private Sub ShowMouseClick_TextChanged(sender As Object, e As EventArgs) Handles ShowMouseClick.TextChanged
If ShowMouseClick.Text = "Left Click" Then
TextBox1.Focus()
SendKeys.Send("{3}")
SendKeys.SendWait("{1}")
End If
End Sub

Related

DataGridView Tooltip showing, even when form not focused

Given: I have a DataGridView list-based application that uses that uses an external Tooltip (default dgv tooltipp is disabled) to display the large content of a specific column on mouseover.
The problem:
It always shows the tooltip on mouseover, even, when the form itself is not in focus.
Scenario:
I'm running my application + Firefox at the same time in non fullscreen mode.
Using my application i get the tooltip I want.
Then i am switching to the half overlaying Firefox.
While my application is behind Firefox it keeps showing the tooltip on top of Firefox, while the form itself is behind it.
Scenario #2:
My application is showing me a tooltip as it is supposed to.
I decide to delete that Gridview entry by pressing del-button on my keyboard which opens a yes/no-msgbox.
the tooltip is displaying on top of the msgbox which makes me unable to use the msgbox buttons with my mouse
The settings:
Code:
Private Sub gridView_CellMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellMouseEnter
If e.ColumnIndex = DirectCast(sender, DataGridView).Columns.Count - 1 And Not e.RowIndex = -1 Then
ToolTip1.SetToolTip(sender, sender.rows(e.RowIndex).cells(e.ColumnIndex).value.ToString)
End If
End Sub
What do i need to do to fix that annoying bug?
Dim isActive As Boolean
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
isActive = True
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
isActive = False
End Sub
At anypoint of time just check
isActive is true of false, if true then form is active.
Source
Other solution would be to subscribe to GetForegroundWindow() and compare return to your forms handle:
Imports System.Runtime.InteropServices
Namespace MyNamespace
Class GFW
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
Public Function IsActive(handle As IntPtr) As Boolean
Dim activeHandle As IntPtr = GetForegroundWindow()
Return (activeHandle = handle)
End Function
End Class
End Namespace
If MyNamespace.GFW.IsActive(Me.Handle) Then
'do whatever
End If

Run Code When The Mouse is Clicked VB

I want certain code to run whenever the mouse is clicked. Most posts I find only execute the code whenever the mouse is clicked inside the form, or inside a certain object. I want the code to run anywhere the mouse is clicked. Is this even possible?
You can use the GetAsyncKeyState api and check the mouse left and right buttons. Here's an example that uses a timer to poll
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If GetAsyncKeyState(Keys.LButton) <> 0 Then
Debug.Print("Left button click")
ElseIf GetAsyncKeyState(Keys.RButton) <> 0 Then
Debug.Print("Right button click")
End If
End Sub
End Class

Mouse hovering event

I want to do an mouse hovering event, when the mouse is over an button I want to change button text color and font size, I have try this code but doesn't work:
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.MediumBlue()
Command1.FontSize = 10
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.ForeColor.White()
Command1.FontSize = 8
End Sub
Can anyone give me a suggestion i have search on Google and try different ways with mouse event handler but didn't work.
First, instead of tracking every mouse move, you can rely on MouseEnter and MouseLeave events of the button.
Second, do not forget to add Handles <Control>.<Event> clause at the declaration of your event-handling procedures.
Result:
Private Sub Command1_MouseEnter(sender As Object, e As EventArgs) _
Handles Command1.MouseEnter
Command1.FontSize = 10
End Sub
Private Sub Command1_MouseLeave(sender As Object, e As EventArgs) _
Handles Command1.MouseLeave
Command1.FontSize = 8
End Sub
Also please do not forget that some users are preferring keyboard control.
This means that
You might want to equip the button with an accelerator.
Command1.Text = "&Launch" (now Alt+L activates the button)
Note: accelerator character for winforms is &, for wpf is _.
You might want to make your entry/leave effect also when the button receives/looses keyboard focus (focus is moved using Tab and Shift+Tab key).
You can try making your changes into MouseEnter and MouseLeave
Private Sub RightButton_MouseEnter(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseEnter
RightButton.ForeColor = Color.AliceBlue
RightButton.Font = New Font(RightButton.Font, 12)
End Sub
Private Sub RightButton_MouseLeave(sender As System.Object, e As System.EventArgs) Handles RightButton.MouseLeave
RightButton.ForeColor = Color.White
RightButton.Font = New Font(RightButton.Font, 10)
End Sub

Basic Key Logger - Code Not Working

Public Class Form1
Dim KeyState
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Int32) As Boolean
Private Sub LogTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogTimer.Tick
For I = 1 To 255
KeyState = 0
KeyState = GetAsyncKeyState(I)
If KeyState = True Then
Me.txtLog.Text = Me.txtLog.Text & Chr(I)
End If
Next I
End Sub
End Class
Just a run down:
I am attempting to get the up/down state of every key every tenth of a second(the timer), then add any keys pressed to a textbox.
I honestly cannot see why this code is not working.
Make sure that you actually have the timer being told to start somewhere. In my experience, I've always had to write actual code to tell it start, and the properties in design view always give me trouble.

Eventhandler "bug" using VB.NET with windows forms

i have the following code:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = &H112
Const SC_SCREENSAVE As Integer = &HF140
MyBase.WndProc(m)
If bloqueado = 0 Then
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_SCREENSAVE Then
Timer2.Start()
inicio = Now
pausa = pausa + 1
AddHandler Application.Idle, AddressOf Application_Idle
End If
End If
End Sub
Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
Dim newitem As ListViewItem
Dim diferença As TimeSpan
'MsgBox(Now.ToString)'
Debug.Print(Now.ToString)
fim = Now
diferença = fim - inicio
Timer2.Stop()
newitem = New ListViewItem
newitem.Text = pausa
newitem.SubItems.Add(inicio.ToLongTimeString)
newitem.SubItems.Add(fim.ToLongTimeString)
newitem.SubItems.Add(diferença.ToString.Substring(0, 8))
ListView1.Items.Add(newitem)
parcial = parcial & pausa & vbTab & vbTab & inicio.ToLongTimeString & vbTab & vbTab & fim.ToLongTimeString _
& vbTab & vbTab & diferença.ToString.Substring(0, 8) & vbTab & vbTab & " screensaver" & System.Environment.NewLine
RemoveHandler Application.Idle, AddressOf Application_Idle
End Sub
Basically the first part detect when screensaver activates and creates a application.idle event handler and the second part, when activity is detected a bunch of code is run and the handler removed.
It's all works fine except for one point:
As you can see i have inicio = now when screensaver becomes active and fim = now when activity is detected (when screensaver becomes inactive), so i should have 2 differente times, but if i have it like i posted the 2 datetime will be the same. If you notice i have a msgbox displaying the now (when screensaver stops) in comment, if i take it out of comment the 2 datetimes will be differente and correct (i used a cronometer to make sure of the results)
Now my questions:
Why does it need the messagebox for the now to be updated and why doesn't it work it debug.print?
Is there a way to solve this problem/update the now var, without having to use a messagebox (i wouldn't like for the app to have pop-up messages)
If i really have to use msgbox for this purpose is there a way for it not to send the pop-up or to autoclick ok right after so it disappears instantly?
EDIT:
I have been searching and i found this code:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Function IsSNRunning() As Boolean
IsSNRunning = (FindWindow("WindowsScreenSaverClass", vbNullString) <> 0)
End Function
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
If IsSNRunning() Then
'Screen Saver Is Running
Else
Timer3.Stop()
code
End If
End Sub
i used Timer3.Start() when in the part that captures the start of the screensaver, my idea being if i start the timer when i know the screensaver if on, then when i get IsSNRunning as false is when the screensaver stops running, but it doesn't work, any ideas why?
Doing anything with Application.Idle is a lost cause. Not only does your app go idle immediately after the screen saver activates, you also never stop being idle while it is running. The screen saver switches the active desktop to a dedicated secure desktop, none of the running programs will ever get any input, not until it de-activates.
You can observe the desktop switch, the SystemEvents.SessionSwitch event fires.
Do note the considerable lack of practical usefulness of code like this. Curiosity is okay but there are always a lot of things to learn. The screen saver should be at the bottom of your list.
First i'll thank you guys for the help, like you said application.idle doesn't work, with you help i got this solution i VB:
Imports System
Imports Microsoft.Win32
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SystemParametersInfo(uAction As UInteger, _
uParam As UInteger, ByRef lpvParam As Boolean, fWinIni As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' Check if the screensaver is busy running.'
Public Shared Function IsScreensaverRunning() As Boolean
Const SPI_GETSCREENSAVERRUNNING As Integer = 114
Dim isRunning As Boolean = False
If Not SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, isRunning, 0) Then
' Could not detect screen saver status...'
Return False
End If
If isRunning Then
' Screen saver is ON.'
Return True
End If
' Screen saver is OFF.'
Return False
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_SYSCOMMAND As Integer = &H112
Const SC_SCREENSAVE As Integer = &HF140
MyBase.WndProc(m)
If bloqueado = 0 Then
If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32 = SC_SCREENSAVE Then
Timer2.Start()
Timer3.Enabled = True
Timer3.Start()
'here we that that the screensaver started running so we start a timer'
End If
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
If IsScreensaverRunning() Then
'Screen Saver Is Running'
Else
Timer3.Stop()
Timer3.Enabled = False
'Screen Saver Is not Running'
End If
End Sub
Because the timer only starts running when the screensaver is running we know that when you get timer3.stop is when the screensaver stopped running
Important, don't put a msgbox before the timer stop because it wont work, the pop-up will show and it wont get to the stop so innumerous pop-up will appear (yeah... i made that mistake :S)
Again, thanks for helping me and hope it will help someone in the future