Disable/lock Keyboard Permanently - vb.net

I new to vb.net. I try to create system to disable/lock keyboard.
And also i try many code from google but its all work only in that form. I want It disable/lock permanently.
Public Class Form1
Private Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Integer) As Integer
Private Declare Function ShowCursor Lib "user32" (ByVal lShow As Long) As Long
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BlockInput(1)
ShowCursor(0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
BlockInput(0)
ShowCursor(1)
End Sub
End Class
Thanks.

Try this but this will block the mouse and the keyboard so i would put an timer to re-enable it
Public Class Form1
Private Declare Function BlockInput Lib "user32" (ByVal fBlock As Long) As Long
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Interval = 5000 '5 seconds
Timer1.Enabled = True
BlockInput(True)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
BlockInput(False)
MsgBox("You can now use your mouse and keyboard")
End Sub
End Class

Related

GetWindowText is returning nothing

In one of my project I have to get the title of foreground window so I called GetForegroundWindow() Entry Point form User32.dll for getting the windows Handle then I called GetWindowText() for the title everything goes error less but the output comes nothing, here is the code I am using in my VB.NET program.
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowText(ByVal hwnd As Long, ByVal lpString As System.Text.StringBuilder, ByVal cch As Long) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hWnd As IntPtr
hWnd = GetForegroundWindow()
Dim title As New System.Text.StringBuilder(256)
GetWindowText(hWnd, title, title.Capacity)
Me.Text = title.ToString
End Sub
End Class
I found the solution myself, It was the fault in the hWnd parameter as Long value for proper functioning of the program it has to be IntPtr. The new correct code looks something like this.
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Long) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim hWnd As IntPtr
hWnd = GetForegroundWindow()
Dim title As New System.Text.StringBuilder(256)
GetWindowText(hWnd, title, title.Capacity)
Me.Text = title.ToString
End Sub
End Class

Hotkeys in Visual Basic 2010: one works, one does not

I'm creating an "autoclicker" for use in video games that left clicks my mouse repeatedly at the push of a button. I want to have hotkeys set up that work globally without the autoclicker window being in focus. For some reason, only the hotkey that is meant to stop the clicking is currently functioning (F2). The F1 key is supposed to start the clicking, but currently the only way to start it is by pressing the Start button on the form manually. The code seems to throw an error when attempting to be deployed to version 4.0 of the .NET framework, but setting it to 3.5 solves this issue. I've included my entire code below because I can't figure out why the F1 hotkey is not functioning despite the F2 hotkey working flawlessly. Thanks for your time. A good portion of this code I came up with by looking at examples of other peoples' work, and I only understand it to a certain extent. I hope my code is formatted correctly because I'm new to this site and I don't exactly understand how to paste it correctly. Thanks for your help in advance, I really appreciate it.
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vkey As Long) As Integer
Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwflags As Long, ByVal dx As Long, ByVal cbuttons As Long, ByVal dy As Long, ByVal dwExtraInfo As Long)
Dim hotkey1 As Boolean
Dim hotkey2 As Boolean
Private Const mouseclickup = 4
Private Const mouseclickdown = 2
Dim Test As Integer
Dim Interval As Integer
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Click.Start()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Click.Stop()
End Sub
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Test = Test + 1
Counter.Text = Test
End Sub
Private Sub Click_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click.Tick
Interval = CInt(timeTextbox.Text)
Click.Interval = Interval
mouse_event(mouseclickdown, 0, 0, 0, 0)
mouse_event(mouseclickup, 0, 0, 0, 0)
hotkey1 = GetAsyncKeyState(Keys.F1)
If hotkey1 = True Then
btnStart.PerformClick()
End If
hotkey2 = GetAsyncKeyState(Keys.F2)
If hotkey2 = True Then
btnStop.PerformClick()
End If
End Sub
Private Sub timeTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeTextbox.TextChanged
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
End Class
Your declarations were for VB6. The data types need to be changed for VB.Net as shown below.
Also, you need two independent timers. One for detecting the keypress, which has to run at a high rate. And another for doing the actual clicking, whose rate is determined by your TextBox value.
Public Class Form1
Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As System.Windows.Forms.Keys) As Short
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
ByVal dwExtraInfo As Integer)
Private WithEvents KeyTimer As New Timer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
KeyTimer.Interval = 50
KeyTimer.Start()
Click.Enabled = False
End Sub
Private Sub KeyTimer_Tick(sender As Object, e As System.EventArgs) Handles KeyTimer.Tick
If IsKeyDown(Keys.F1) Then
Click.Start()
ElseIf IsKeyDown(Keys.F2) Then
Click.Stop()
End If
End Sub
Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Static Test As Integer
Test = Test + 1
Counter.Text = Test
End Sub
Private Sub timeTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeTextbox.TextChanged
Dim Interval As Integer
If Integer.TryParse(timeTextbox.Text, Interval) Then
Click.Interval = Interval
End If
End Sub
Private Sub Click_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click.Tick
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
Private Function IsKeyDown(ByVal key As System.Windows.Forms.Keys) As Boolean
Return ((GetAsyncKeyState(key) And &H8000) = &H8000)
End Function
End Class

Exception.InnerException in vb.net

This is my first time making a program using visual basic language. I thought my project is running smooth because every time I debug using visual studio the program is ok so I didn't mind checking the .exe file of the program but today I've found this error in the .exe file of the program
An error occured creating the form. See Exception.InnerException for details. The Error is: Could not load file or assembly 'AxInterop.WMPLib.Version=1.0.0.0 Culture=neutral. PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified
I don't understand why the program is running good in visual studio. but has errors in .exe
Here is the code of the form that has error
Public Class formVideo
Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2,
(Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2)
Call Disable(Me)
End Sub
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal revert As Integer) As Integer
Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer
Private Const SC_CLOSE As Integer = &HF060
Private Const MF_BYCOMMAND As Integer = &H0
Private Const MF_GRAYED As Integer = &H1
Private Const MF_ENABLED As Integer = &H0
Public Shared Sub Disable(ByVal form As System.Windows.Forms.Form)
' The return value specifies the previous state of the menu item (it is either
' MF_ENABLED or MF_GRAYED). 0xFFFFFFFF indicates that the menu item does not exist.
Select Case EnableMenuItem(GetSystemMenu(form.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED)
Case MF_ENABLED
Case MF_GRAYED
Case &HFFFFFFFF
Throw New Exception("The Close menu item does not exist.")
Case Else
End Select
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Call Disable(Me)
End Sub
Private Sub btnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMenu.Click
Me.Close()
formMenu.Show()
End Sub
Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
mpVideo.Ctlcontrols.play()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
mpVideo.Ctlcontrols.pause()
End Sub
Private Sub btnCPU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCPU.Click
mpVideo.URL = "Resources\See How the CPU Works In One Lesson.avi"
End Sub
Private Sub btnMobo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMobo.Click
mpVideo.URL = "Resources\Understanding your motherboard.avi"
End Sub
Private Sub btnHDD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHDD.Click
mpVideo.URL = "Resources\What is a Hard Drive_ (HDD vs SSD_) - Computer Basics.avi"
End Sub
Private Sub btnRam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRam.Click
mpVideo.URL = "Resources\What is RAM_ - Computer Basics.avi"
End Sub
Private Sub btnPSU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPSU.Click
mpVideo.URL = "Resources\Testing the PC's PSU.avi"
End Sub
Private Sub btnVGA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVGA.Click
mpVideo.URL = "Resources\What is a GPU_ - Computer Basics.avi"
End Sub
Private Sub btnCooling_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCooling.Click
mpVideo.URL = "Resources\A Beginner's Guide to Water Cooling Your Computer.avi"
End Sub
Private Sub btnExtensions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExtensions.Click
mpVideo.URL = "Resources\Peripheral - Wiki Article.avi"
End Sub
Private Sub mpVideo_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mpVideo.Enter
mpVideo.stretchToFit = True
End Sub
Private Sub btnFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFF.Click
mpVideo.Ctlcontrols.fastForward()
End Sub
Private Sub btnRewind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRewind.Click
mpVideo.Ctlcontrols.fastReverse()
End Sub
End Class
Ok. Make sure that axinterop.dll is present on the directory that the solution file refers to, before compiling. Depending on which OS you have, will depend on how Visual Studio works. The Exception only triggers on a runtime error occurrence. And also, the compiled .exe file, has to be with the .pdb file and the .config file. Otherwise, it would not work. If all else fails, then reboot your device, and if it still fails after that, then it might be a complete internal runtime error.
Make sure that the axinterop dll is present in the application folder. To me it sounds like you only copied the executable from you bin folder.

VB.NET | How to get Windows Form WorkingArea, not Desktop WorkingArea

I wanted an effect of fading a panel control and the controls / object inside it. I found some containers with opacity property it only changes the background color's opacity.
So I came up my own solution. I created another form containing the controls I need and I got what I want but I got some problems with positioning the new form created. I don't know how to get the working area of its parent form to set the initial position. What I mean for working area is that, it won't include the control box / title bar. Different OS has different title bar sizes (as far as I know) so I need to adjust it correctly
Form2, the one holding the Opaque Controls
Public Class Form2
Private Const dif As Integer = 23
Private Const GWL_EXSTYLE As Integer = (-20)
Private Const WS_EX_TRANSPARENT As Integer = &H20
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
Public Sub resizeFrom(ByVal parent As Form)
Me.Height = parent.Height - dif
Me.Width = parent.Width
Me.Top = (parent.Top + ((parent.Height - Me.Height) / 2) + (dif / 2))
Me.Left = parent.Left + ((parent.Width - Me.Width) / 2)
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TopMost = True
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.BackColor = Color.Red
SetWindowLong(Me.Handle, GWL_EXSTYLE, GetWindowLong(Me.Handle, GWL_EXSTYLE) Or WS_EX_TRANSPARENT)
End Sub
End Class
The Parent/Main Form
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Form2.Show()
Form2.resizeFrom(Me)
End Sub
Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
Form2.resizeFrom(Me)
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Form1_Move(Me, New EventArgs)
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
Form2.Opacity = HScrollBar1.Value / 100
Me.Text = "Opacity: " & HScrollBar1.Value & "%"
End Sub
End Class
It should look like this
http://i48.tinypic.com/25qubk0.jpg
Opaque Form is for display purposes, it was set to "Ghost Like Form" or "Click-through Form"
Is there any solution so I can get the proper WorkingArea of the Form?
Please help.
Thanks
What you need is the absolute location of the ClientRectangle. The Location of this is (0, 0) but you can magically convert it to screen values:
Form2.Location = PointToScreen(Me.ClientRectangle.Location)
Form2.Size = Me.ClientRectangle.Size

Change cursor VB.NET

I can't change the cursor when it's a ToolStripButton.click event.
I got 2 buttons that call "Rechercher".
EDITED : Only the Button is working. It seems that the ToolStripButton cancel my cursor...
Thx for the help
Public Class FenetrePrincipale
Private WithEvents _btnRechercher As New Windows.Forms.ToolStripButton("Rechercher")
Private WithEvents btnRechercherAccesBtn As New Button
Private Sub Rechercher(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btnRechercher.Click, btnRechercherAccesBtn.Click
Try
Me.Cursor = Cursors.WaitCursor
'WAITING FOR THE CODE TO FINISH (2 sec)
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
End Class
Maybe you should try something sampler like:
Private Sub MainFrame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Search()
End Sub
Private Sub Search()
Try
Me.Cursor = Cursors.WaitCursor
UseWaitCursor = True
Application.DoEvents()
Threading.Thread.Sleep(1000) 'WAITING FOR THE CODE TO FINISH
Finally
UseWaitCursor = False
Me.Cursor = Cursors.Default
Application.DoEvents()
End Try
End Sub
The problem is you don't have any pause where code should execute so it is doing to fast.
This is the only way I got this to work.
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Public Class FenetrePrincipale
Private WithEvents _btnRechercher As New Windows.Forms.ToolStripButton("Rechercher")
Private WithEvents btnRechercherAccesBtn As New Button
Private Sub Rechercher(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercherAccesBtn.Click
Try
Me.Cursor = Cursors.WaitCursor
'code...
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Private Sub RechercherToolStripButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnRechercher.Click
Me.UseWaitCursor = True
SendMessage(Me.Handle, &H20, Me.Handle, New IntPtr(1))
Rechercher(Nothing, Nothing)
Me.UseWaitCursor = False
End Sub
End Class