keypress when the form is hidden/ minimize?

1icax

New member
Joined
May 14, 2024
Messages
2
Programming Experience
Beginner
HELO EVERYONE

this windows form and mysql

Screenshot 2024-05-14 174538.png
Screenshot 2024-05-14 174753.png


code form1

form1:
Public Class Form1
    Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        lcom.Text = Environment.MachineName
    End Sub
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Me.WindowState = FormWindowState.Minimized
        Me.Visible = False
        e.Cancel = True
    End Sub
    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.F9
                con()
                cmd.Connection = conn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "insert into info ( jam, tanggal, computer) VALUES ('" & Format(Now, "HH:mm:ss") & "','" & Format(Now, "yyyy-MM-dd") & "','" & lcom.Text & "')"
                cmd.ExecuteNonQuery()
        End Select
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lcom.Text = Environment.MachineName
        ldatetime.Text = Now
    End Sub

I made a simple program with VB Net and MySQL

when the f9 button is pressed it immediately saves to the database

when pressed it is saved to the database

the problem
when the form is hidden/ minimize the f9 button cannot save to the database

Please help
 
Of course it works that way. You are detecting when a particular key press is sent to your form. If your form doesn't have focus, whether it's hidden or not, any key presses you make will not be sent to that form so you can't detect them like that.

It's possible to register a key combination with Windows so that, any time Windows detects that key combination, it will send a message to your form. You absolutely would not - possibly cannot - do that with a single key like F9 though. If your app doesn't have focus then some other app does and there's every chance that the F9 key means something to it, so you should not be forcing the user to press a key that could have some random effect in some random app. If you do want to register a hotkey like that, you should use at least two modifiers, e.g. Ctrl+Shift, so that the chance of clashing with a key combination that means something in another app is slim. If you're interested in doing that, do some research on the RegisterHotKey API.
 
By the way, you should absolutely not be inserting vaues into SQL code that way. You should ALWAYS use parameters, fopr m,ultiple reasons. For more info on why and how, follow the Blog link in my signature below and check out my post on Parameters in ADO.NET.
 
If you were going to build a String that way though, use string interpolation for improved readability:
VB.NET:
cmd.CommandText = $"INSERT INTO info (jam, tanggal, computer) VALUES ('{Date.Now:HH:mm:ss}','{Date.Now:yyyy-MM-dd}','{lcom.Text}')"
 
Of course it works that way. You are detecting when a particular key press is sent to your form. If your form doesn't have focus, whether it's hidden or not, any key presses you make will not be sent to that form so you can't detect them like that.

It's possible to register a key combination with Windows so that, any time Windows detects that key combination, it will send a message to your form. You absolutely would not - possibly cannot - do that with a single key like F9 though. If your app doesn't have focus then some other app does and there's every chance that the F9 key means something to it, so you should not be forcing the user to press a key that could have some random effect in some random app. If you do want to register a hotkey like that, you should use at least two modifiers, e.g. Ctrl+Shift, so that the chance of clashing with a key combination that means something in another app is slim. If you're interested in doing that, do some research on the RegisterHotKey API.

OK sir, I will study it
 

Latest posts

Back
Top