Powershellでフォームのラベルを試していたところ、ラベルを縦長に複数配置するとピアノの鍵盤のように成りました。
同様に背景色を黒色のラベルで黒鍵を表現しています。
せっかくなのでキーボードのキーを押すと音が鳴ると楽しそうなので試行錯誤してみました。
Powershellから音を鳴らす方法はあまり多くなくBeep音を鳴らす方法とwavファイルを再生する方法が見つかりました。
Beep音は予め再生時間を指定する必要があり、キーが離れるイベントで再生を停止する方法がなさそうなので、wavファイルを再生するようにしてみました。
キーボードのASDFGHJKにドレミファソラシドをわりあて、キーを押すとwavファイルをループ再生します。キーを離すとwavファイルの再生を停止します。
私の環境では一応音が鳴りましたが、ゆっくりキーの押しこみ音が鳴った事を確認したうえでキーを離す必要があります。楽器としてはお話にならない代物ですが、フォームのラベルとwavファイルの再生、キーイベントのサンプルだとお考え下さい。
wavファイルについて
スクリプト
<#
鍵盤的なにか
#>
# 音声ファイルは以下から拝借
#
# https://maoudamashii.jokersounds.com/list/se12.html
Set-StrictMode -Version Latest
$ErrorActionPreference = "STOP"
Add-Type -AssemblyName System.Windows.Forms
function main {
$c = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_1do.wav")
$d = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_2re.wav")
$e = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_3mi.wav")
$f = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_4fa.wav")
$g = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_5so.wav")
$a = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_6ra.wav")
$b = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_7si.wav")
$c2 = New-Object Media.SoundPlayer("C:\Users\karet\Documents\WindowsPowerShell\Form\keyboad\se_maoudamashii_instruments_piano1_8do.wav")
$form = New-Object System.Windows.Forms.Form
$form.Name = "FORM"
$label = New-Object System.Windows.Forms.Label
$label.Location = "10,10"
$label.Size = "30,120"
$label.Text = ""
$label.BorderStyle = "FixedSingle"
$label.BackColor = "White"
$label.Name = "C"
$label.TextAlign = "BottomCenter"
$label3 = New-Object System.Windows.Forms.Label
$label3.Location = "40,10"
$label3.Size = "30,120"
$label3.Text = ""
$label3.BorderStyle = "FixedSingle"
$label3.BackColor = "White"
$label3.Name = "D"
$label3.TextAlign = "BottomCenter"
$label4 = New-Object System.Windows.Forms.Label
$label4.Location = "70,10"
$label4.Size = "30,120"
$label4.Text = ""
$label4.BorderStyle = "FixedSingle"
$label4.BackColor = "White"
$label4.Name = "E"
$label4.TextAlign = "BottomCenter"
$label5 = New-Object System.Windows.Forms.Label
$label5.Location = "100,10"
$label5.Size = "30,120"
$label5.Text = ""
$label5.BorderStyle = "FixedSingle"
$label5.BackColor = "White"
$label5.Name = "F"
$label5.TextAlign = "BottomCenter"
$label6 = New-Object System.Windows.Forms.Label
$label6.Location = "130,10"
$label6.Size = "30,120"
$label6.Text = ""
$label6.BorderStyle = "FixedSingle"
$label6.BackColor = "White"
$label6.Name = "G"
$label6.TextAlign = "BottomCenter"
$label7 = New-Object System.Windows.Forms.Label
$label7.Location = "160,10"
$label7.Size = "30,120"
$label7.Text = ""
$label7.BorderStyle = "FixedSingle"
$label7.BackColor = "White"
$label7.Name = "A"
$label7.TextAlign = "BottomCenter"
$label8 = New-Object System.Windows.Forms.Label
$label8.Location = "190,10"
$label8.Size = "30,120"
$label8.Text = ""
$label8.BorderStyle = "FixedSingle"
$label8.BackColor = "White"
$label8.Name = "B"
$label8.TextAlign = "BottomCenter"
$label9 = New-Object System.Windows.Forms.Label
$label9.Location = "220,10"
$label9.Size = "30,120"
$label9.Text = ""
$label9.BorderStyle = "FixedSingle"
$label9.BackColor = "White"
$label9.Name = "CC"
$label9.TextAlign = "BottomCenter"
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = "30,10"
$label2.Size = "20,70"
$label2.BorderStyle = "FixedSingle"
$label2.BackColor = "Black"
$label10 = New-Object System.Windows.Forms.Label
$label10.Location = "60,10"
$label10.Size = "20,70"
$label10.BorderStyle = "FixedSingle"
$label10.BackColor = "Black"
$label11 = New-Object System.Windows.Forms.Label
$label11.Location = "120,10"
$label11.Size = "20,70"
$label11.BorderStyle = "FixedSingle"
$label11.BackColor = "Black"
$label12 = New-Object System.Windows.Forms.Label
$label12.Location = "150,10"
$label12.Size = "20,70"
$label12.BorderStyle = "FixedSingle"
$label12.BackColor = "Black"
$label13 = New-Object System.Windows.Forms.Label
$label13.Location = "180,10"
$label13.Size = "20,70"
$label13.BorderStyle = "FixedSingle"
$label13.BackColor = "Black"
$form.Controls.Add($label13)
$form.Controls.Add($label12)
$form.Controls.Add($label11)
$form.Controls.Add($label10)
$form.Controls.Add($label2)
$form.Controls.Add($label9)
$form.Controls.Add($label8)
$form.Controls.Add($label7)
$form.Controls.Add($label6)
$form.Controls.Add($label5)
$form.Controls.Add($label4)
$form.Controls.Add($label3)
$form.Controls.Add($label)
$form.Add_KeyDown({
Switch ($_.KeyCode) {
"A" {
$this.Controls["C"].Text = "ド"
$c.PlayLooping()
}
"S" {
$this.Controls["D"].Text = "レ"
$d.PlayLooping()
}
"D" {
$this.Controls["E"].Text = "ミ"
$e.PlayLooping()
}
"F" {
$this.Controls["F"].Text = "ファ"
$f.PlayLooping()
}
"G" {
$this.Controls["G"].Text = "ソ"
$g.PlayLooping()
}
"H" {
$this.Controls["A"].Text = "ラ"
$a.PlayLooping()
}
"J" {
$this.Controls["B"].Text = "シ"
$b.PlayLooping()
}
"K" {
$this.Controls["CC"].Text = "ド"
$c2.PlayLooping()
}
}
})
$form.Add_KeyUp({
Switch ($_.KeyCode) {
"A" {
$this.Controls["C"].Text = ""
$c.Stop()
}
"S" {
$this.Controls["D"].Text = ""
$d.Stop()
}
"D" {
$this.Controls["E"].Text = ""
$e.Stop()
}
"F" {
$this.Controls["F"].Text = ""
$f.Stop()
}
"G" {
$this.Controls["G"].Text = ""
$g.Stop()
}
"H" {
$this.Controls["A"].Text = ""
$a.Stop()
}
"J" {
$this.Controls["B"].Text = ""
$b.Stop()
}
"K" {
$this.Controls["CC"].Text = ""
$c2.Stop()
}
}
})
$form.ShowDialog()
$c.Dispose()
$d.Dispose()
$e.Dispose()
$f.Dispose()
$g.Dispose()
$a.Dispose()
$b.Dispose()
$c2.Dispose()
}
main
コメント