Hey guys!
First of all, if I'm breaking any norms or anything similar to that, please do tell what I'm doing wrong.
Now to my problem:
I'm trying to do a chat bot for twitch which will listen to commands from the chat and then respond to what they're writing. In this case i want them to be able to change song and my plan was at first to use SendKeys, but that forces the spotify window to be focused (which is not what i want, that would interrupt the gaming) so i later resolved to use SendMessage commands. So what the API does is it that i first manually enter the Search bar handle and then set the text to whatever the viewer is writing. After that the API presses enter, i press down/up arrow key to select the song which is the furthest, then playing the song.
The SendMessage works in all cases currently but one: Pressing the down/up key or pressing the tab key (accessing the tab function). It does though if i have the window focused, which i don't understand at all...
So in short: Everything works right now but selecting the song. Please help.
The code looks like dogpoo right now only because i'm just trying to fix it by just throwing different variations in there, but yet I'm failing.
Any help would be greatly appreciated
First of all, if I'm breaking any norms or anything similar to that, please do tell what I'm doing wrong.
Now to my problem:
I'm trying to do a chat bot for twitch which will listen to commands from the chat and then respond to what they're writing. In this case i want them to be able to change song and my plan was at first to use SendKeys, but that forces the spotify window to be focused (which is not what i want, that would interrupt the gaming) so i later resolved to use SendMessage commands. So what the API does is it that i first manually enter the Search bar handle and then set the text to whatever the viewer is writing. After that the API presses enter, i press down/up arrow key to select the song which is the furthest, then playing the song.
The SendMessage works in all cases currently but one: Pressing the down/up key or pressing the tab key (accessing the tab function). It does though if i have the window focused, which i don't understand at all...
So in short: Everything works right now but selecting the song. Please help.
The code looks like dogpoo right now only because i'm just trying to fix it by just throwing different variations in there, but yet I'm failing.
Code:
Public Class SpotifyController
#Region " win32 "
Private Declare Auto Function SetForegroundWindow Lib "user32" (ByVal hWnd As IntPtr) As Boolean
Private Declare Auto Function keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) As Boolean
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As IntPtr) As IntPtr
Private Declare Auto Function SetWindowText Lib "user32" (ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean
Private Declare Auto Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd As IntPtr, ByVal hWndChildAfterA As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
Const WM_SETTEXT As Integer = &HC
'Set window text by string
Private Declare Function SendMessageByString Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
#End Region
#Region " constants "
Private Const WM_KEYDOWN = &H1000
Private Const WM_KEYUP = &H101
Private Const WM_MOUSEACTIVATE = &H21
Private Const KEYEVENTF_EXTENDEDKEY As Integer = &H1S
Private Const KEYEVENTF_KEYUP As Integer = &H2S
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const BM_CLICK = &HF5
Private Const VK_TAB = &H9
Private Const VK_ALT = 18
Public Const WM_SETFOCUS = &H7
Const VK_UP As Integer = &H26
Const VK_DOWN As Integer = &H28
#End Region
' Get window text length signature.
'Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
' ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As IntPtr) As IntPtr
Private spotifyWindow As Integer
Private searchField As Integer
Private wowWindow As Integer
Public Sub Search(ByVal _LineText As String)
Dim LineText As String
LineText = _LineText
spotifyWindow = FindWindow("SpotifyMainWindow", vbNullString)
searchField = FindWindow("SPA69704", spotifyWindow)
wowWindow = FindWindow("GxWindowClass", vbNullString)
Dim ControlWindowHandle As IntPtr = CType(spotifyWindow, IntPtr)
'Searching for song
Console.WriteLine("**SEARCHING FOR SONG**")
SendMessageByString(<Spotify Search Field>, WM_SETTEXT, 0&, LineText)
Threading.Thread.Sleep(200)
SendMessage(<Spotify Search Field>, WM_KEYDOWN, System.Windows.Forms.Keys.Return, vbNullString)
Threading.Thread.Sleep(200)
SendMessage(<Spotify Search Field>, WM_KEYUP, System.Windows.Forms.Keys.Return, vbNullString)
Console.WriteLine("**I dont even know if i care anymore...**")
Threading.Thread.Sleep(2000)
'Targeting the song
SendMessage(<Spotify Main Window>, WM_KEYDOWN, System.Windows.Forms.Keys.Up, vbNullString)
Threading.Thread.Sleep(200)
SendMessage(<Spotify Main Window>, WM_KEYUP, System.Windows.Forms.Keys.Up, vbNullString)
Console.WriteLine("**SONG SELECTED, PLAYING (PRESSING ENTER)**")
SendMessage(<Spotify Main Window>, WM_SETFOCUS, 0&, IntPtr.Zero)
SendMessage(<Spotify Main Window>, WM_KEYDOWN, System.Windows.Forms.Keys.Return, 0)
Threading.Thread.Sleep(200)
SendMessage(<Spotify Main Window>, WM_KEYUP, System.Windows.Forms.Keys.Return, 0)
Console.WriteLine("**SONG PLAYING?**")
End Sub
End Class