193 lines
7.4 KiB
VB.net
193 lines
7.4 KiB
VB.net
Imports System.Runtime.InteropServices
|
|
Imports System.Text
|
|
Imports System.Media
|
|
Imports System.Xml.Linq
|
|
Imports System.IO
|
|
'Imports NAudio.Wave
|
|
|
|
|
|
'Public Class AudioPlayer
|
|
' Private player As IWavePlayer
|
|
' Private reader As AudioFileReader
|
|
|
|
' Public Sub Play(filePath As String)
|
|
' Try
|
|
' player = New WaveOutEvent() ' Usa DirectSound
|
|
' reader = New AudioFileReader(filePath)
|
|
' player.Init(reader)
|
|
' player.Play()
|
|
' Catch ex As Exception
|
|
' MessageBox.Show($"Errore durante la riproduzione: {ex.Message}")
|
|
' End Try
|
|
' End Sub
|
|
|
|
' Public Sub StopPlayback()
|
|
' player?.Stop()
|
|
' reader?.Dispose()
|
|
' player?.Dispose()
|
|
' End Sub
|
|
'End Class
|
|
|
|
Public Class Form1
|
|
' Costanti per l'hook della tastiera
|
|
Private Const WH_KEYBOARD_LL As Integer = 13
|
|
Private Const WM_KEYDOWN As Integer = &H100
|
|
|
|
' Delegati e handle dell'hook
|
|
Private Delegate Function HookProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
|
|
Private Shared _hookID As IntPtr = IntPtr.Zero
|
|
Private Shared _proc As HookProc = AddressOf HookCallback
|
|
Private Shared setup As Boolean = False
|
|
|
|
' Dizionario per mappare i tasti ai file WAV
|
|
Private Shared keySoundMapping As New Dictionary(Of Keys, String)()
|
|
|
|
' Variabile per tracciare se siamo nella fase di selezione di un file per un tasto
|
|
Private Shared isSelectingFile As Boolean = False
|
|
|
|
' Percorso del file XML di configurazione
|
|
Private Shared configFilePath As String = "key_sound_mapping.xml"
|
|
|
|
' Importazioni delle API di Windows
|
|
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
|
|
Private Shared Function SetWindowsHookEx(idHook As Integer, lpfn As HookProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
|
|
End Function
|
|
|
|
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
|
|
Private Shared Function UnhookWindowsHookEx(hhk As IntPtr) As Boolean
|
|
End Function
|
|
|
|
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
|
|
Private Shared Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
|
|
End Function
|
|
|
|
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
|
|
Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
|
|
End Function
|
|
|
|
' Metodo per installare l'hook
|
|
Private Sub InstallHook()
|
|
Dim hMod = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName)
|
|
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, hMod, 0)
|
|
If _hookID = IntPtr.Zero Then
|
|
MessageBox.Show("Errore nell'impostazione dell'hook.")
|
|
End If
|
|
End Sub
|
|
|
|
' Metodo per rimuovere l'hook
|
|
Private Sub RemoveHook()
|
|
If _hookID <> IntPtr.Zero Then
|
|
UnhookWindowsHookEx(_hookID)
|
|
End If
|
|
End Sub
|
|
|
|
' Callback dell'hook
|
|
Private Shared Function HookCallback(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
|
|
If nCode >= 0 AndAlso wParam = CType(WM_KEYDOWN, IntPtr) Then
|
|
Dim vkCode = Marshal.ReadInt32(lParam)
|
|
Dim key = CType(vkCode, Keys)
|
|
|
|
If setup Then
|
|
' Modalità setup: mappa il tasto al file WAV
|
|
If Not isSelectingFile Then
|
|
isSelectingFile = True ' Inizia la selezione del file
|
|
Dim openFileDialog As New OpenFileDialog With {
|
|
.Filter = "File Audio WAV (*.wav)|*.wav",
|
|
.Title = $"Seleziona il file WAV per il tasto {key.ToString()}"
|
|
}
|
|
|
|
If openFileDialog.ShowDialog() = DialogResult.OK Then
|
|
Dim selectedFilePath As String = openFileDialog.FileName
|
|
|
|
' Aggiunge o aggiorna la mappatura
|
|
If keySoundMapping.ContainsKey(key) Then
|
|
keySoundMapping(key) = selectedFilePath
|
|
Else
|
|
keySoundMapping.Add(key, selectedFilePath)
|
|
End If
|
|
|
|
' Salva il mapping in un file XML
|
|
SaveMappingsToFile()
|
|
|
|
MessageBox.Show($"Associato il file '{selectedFilePath}' al tasto {key}.")
|
|
End If
|
|
|
|
isSelectingFile = False ' Finisce la selezione del file
|
|
End If
|
|
Else
|
|
' Modalità normale: riproduce il suono associato
|
|
If keySoundMapping.ContainsKey(key) Then
|
|
Dim soundFilePath = keySoundMapping(key)
|
|
Dim soundPlayer As New SoundPlayer(soundFilePath)
|
|
soundPlayer.Play()
|
|
|
|
' Aggiungi log nella ListBox
|
|
Current.ListBox1.Items.Add($"Premuto tasto: {key}, suono riprodotto.")
|
|
Else
|
|
' Log nel caso non sia stato associato un suono
|
|
Current.ListBox1.Items.Add($"Nessun suono associato al tasto {key}.")
|
|
End If
|
|
Current.ListBox1.SelectedIndex = Current.ListBox1.Items.Count - 1
|
|
End If
|
|
End If
|
|
|
|
Return CallNextHookEx(_hookID, nCode, wParam, lParam)
|
|
End Function
|
|
|
|
Public Shared Current As Form1
|
|
|
|
' Metodo per salvare la mappatura dei tasti su file XML
|
|
Private Shared Sub SaveMappingsToFile()
|
|
' Crea o aggiorna il file XML con la mappatura dei tasti
|
|
Dim xmlDoc As New XDocument(
|
|
New XElement("Mappings",
|
|
From entry In keySoundMapping
|
|
Select New XElement("Mapping",
|
|
New XElement("Key", entry.Key.ToString()),
|
|
New XElement("FilePath", entry.Value))
|
|
)
|
|
)
|
|
|
|
' Salva il file XML
|
|
xmlDoc.Save(configFilePath)
|
|
LoadMappingsFromFile()
|
|
End Sub
|
|
|
|
' Metodo per caricare le mappature dei tasti da file XML
|
|
Private Shared Sub LoadMappingsFromFile()
|
|
If File.Exists(configFilePath) Then
|
|
Dim xmlDoc As XDocument = XDocument.Load(configFilePath)
|
|
|
|
keySoundMapping = (From mapping In xmlDoc.Descendants("Mapping")
|
|
Let key = CType([Enum].Parse(GetType(Keys), mapping.Element("Key").Value), Keys)
|
|
Let filePath = mapping.Element("FilePath").Value
|
|
Select KeyValuePair.Create(key, filePath)).ToDictionary(Function(x) x.Key, Function(x) x.Value)
|
|
|
|
' Popola ListBox2 con i tasti e i relativi percorsi
|
|
Current.ListBox2.Items.Clear()
|
|
For Each kvp As KeyValuePair(Of Keys, String) In keySoundMapping
|
|
Current.ListBox2.Items.Add($"{kvp.Key.ToString()} - {kvp.Value}")
|
|
Next
|
|
End If
|
|
End Sub
|
|
|
|
' Eventi del form per installare/rimuovere l'hook
|
|
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
|
|
InstallHook()
|
|
Current = Me
|
|
Button1.Text = "Setup = " & setup.ToString()
|
|
|
|
' Carica la mappatura dei tasti all'avvio e popola ListBox2
|
|
LoadMappingsFromFile()
|
|
End Sub
|
|
|
|
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
|
|
RemoveHook()
|
|
End Sub
|
|
|
|
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
|
setup = Not setup
|
|
Button1.Text = "Setup = " & setup.ToString()
|
|
End Sub
|
|
End Class
|