如何在Vb.Net中使用Sendkey模拟向下键 [英] How Can I Simulate The Down Key Using Sendkey In Vb.Net

查看:133
本文介绍了如何在Vb.Net中使用Sendkey模拟向下键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了这个vb.net程序,我从列表框中的特定文件夹中加载了所有歌曲。我有两个按钮 - button1用于加载歌曲,button2是我打算用来模拟向下箭头键,这样每当我点击button2,我就会转到下一首歌曲。但问题是,我使用计时器模拟向下箭头键,每当我点击按钮2时,计时器启动,向下箭头键不停地继续前进。这不是我想要的,我的目标是当点击button2时,它应该只走一步而不是继续前进。我尝试使用循环来阻止它但没有任何效果。我知道如何解决这个问题。

继承我的代码 -

 私人  Sub  Button2_Click(发件人作为 对象 ,e  As  EventArgs) Handles  Button2.Click 


Timer1 .Enabled = True

结束 Sub

私有 Sub Timer1_Tick(发件人作为 对象,e As EventArgs)句柄 Timer1.Tick

SendKeys.Send( {DOWN}

结束 Sub

解决方案

您可以将其添加为解决方案,因为我已对此进行了测试及其工作正常。希望能帮助到你。



 私人  Sub  Form1_Load(发件人作为 对象,e 作为 EventArgs)句柄  MyBase  .Load ' 当formloads  
AddHandler eTB.KeyDown, AddressOf eTB_Ev ' 为我们在代码中创建的文本框添加处理程序。
.Controls.Add(eTB)' 现在添加控件
结束 Sub

< span class =code-keyword> Dim eWin_KeyDown As 整数 =& H100 ' 声明为API
Dim eWin_KeyUp 作为 整数 =& H101 ' 相同^

公共 声明 功能 eAPISMessage Lib user32.dll 别名 SendMessageA ByVal hWnd 作为 整数 ByVal wMsg 作为 整数 ByVal wParam 作为 整数 ByVal lParam As String 作为 整数
' 上述函数将用于执行send命令。

公共 WithEvents eTB 作为 TextBox ' 创建文本框的实例。

私有 Sub Button2_Click(发件人作为 对象 ,e As EventArgs )句柄 Button2.Click
eTB.Focus()' 给TextBox焦点。
eAPISMessage(eTB.Handle.ToInt32,eWin_KeyDown,eWin_KeyUp, CStr 0 ))' 使用我们的API调用,我们可以将keydown的消息发送到keydown事件
结束 Sub

私有 Sub eTB_Ev( ByVal sender As 对象 ByVal e As System.Windows.Forms.KeyEventArgs)
' 不要添加处理程序,因为API函数是调用通过这个子并通过按键。 (添加句柄eTB.KeyPress只会让你接收两次。)
MsgBox( 从Command3按钮按下Keydown。' 一旦你感到满意就移除它。
尝试 ' 使用try块来捕获您可能遇到的任何索引异常错误。

' 我也不喜欢检查特定键按下的按键,因为它通过API函数传递。

如果 ListBox1.SelectedIndex = 0 然后 ' 我们将检查是否会导致异常的zer索引。
退出 Sub ' 如果没有要索引的项目,则退出子
否则
.ListBox1.SelectedIndex = Me .ListBox1.SelectedIndex + 1 ' 选择下一个项目。
结束 如果
Catch ex As 异常
' 您可以在此处理错误或忽略它。忽略这一点是安全的。 ;)
结束 尝试
结束 Sub
私人 Sub ListBox1_SelectedIndexChanged(发件人作为 对象,e 作为 EventArgs)句柄 ListBox1.SelectedIndexChanged
' 当索引更改时,将触发。
' 将您的代码放在这里播放下一首歌。
结束 Sub


I made this vb.net program where i loaded all my songs from a particular folder in a listbox. I have two buttons- button1 is for loading the songs and button2 is what i intended for is simulating the down arrow key, so that whenever i click on button2, i go to the next song. But here is the problem, i used a timer to simulate the down arrow key and whenever i click on the button2 , the timer starts and the down arrow key keeps going continuously without stopping. This is not what i want, my goal is that the when the click on button2, it should just go 1 step and not keep going continuously. I tried using loops to stop that but nothing worked. Any idea how i can solve this issue.
Heres my code-

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


        Timer1.Enabled = True

 End Sub

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

SendKeys.Send("{DOWN}")

End Sub

解决方案

You can add this as a solution, as I have tested this and its working fine. Hope it helps.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'When formloads
    AddHandler eTB.KeyDown, AddressOf eTB_Ev 'Add a handler for the textbox we created in code.
    Me.Controls.Add(eTB) 'Now add the control
End Sub

Dim eWin_KeyDown As Integer = &H100 'Declared for API
Dim eWin_KeyUp As Integer = &H101 'Same ^

Public Declare Function eAPISMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
'The function above will be used to execute the send command.

Public WithEvents eTB As New TextBox 'Create an instance of a textbox.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    eTB.Focus() 'Give the TextBox Focus.
    eAPISMessage(eTB.Handle.ToInt32, eWin_KeyDown, eWin_KeyUp, CStr(0)) 'Using our API call we can send the message of keydown to the keydown event
End Sub

Private Sub eTB_Ev(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    'Dont add a handler because the API function is calling this sub and passing the key press. (Adding Handles eTB.KeyPress will only cause you to receive it twice.)
    MsgBox("Keydown pressed from Command3 button.") 'Remove this once you are happy it works.
    Try 'Use a try block to catch any index exception errors which you might have.

        'I also don't check for a key press for a specific key press because its passed by the API function.

        If ListBox1.SelectedIndex = 0 Then 'We will check for a zer index whcih would cause an exception.
            Exit Sub 'Exit the sub if there are no items to index
        Else
            Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1 'Select next Item.
        End If
    Catch ex As Exception
        'You can handle your error here or ignore it. It's safe to ignore this one. ;)
    End Try
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    'When the index is changed, this will fire.
    'Put your code here to play next song.
End Sub


这篇关于如何在Vb.Net中使用Sendkey模拟向下键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆