User Forum    :: Powered by YaBB
  « MIDI-OX User Forum - generate key press from midi events »
Welcome, Guest. Please Login or Register.
May 9th, 2025, 8:47am


Home Home Help Help Search Search Members Members Login Login Register Register


   MIDI-OX User Forum
   MIDI-OX
   Scripting Questions
(Moderator: Jamie OConnell)
   generate key press from midi events
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: generate key press from midi events  (Read 6131 times)
kotto03
New Member
*



MIDI-OX Rules!

   


Posts: 5
generate key press from midi events
« on: Dec 28th, 2003, 8:43am »
Quote Quote Modify Modify

Hi
I have one question. Is it possible to generate a keypress (I mean e.g. a,b,c or space key) event from a midi event and send this keypress event to the active window?
 
 
kotto
 
IP Logged
Peter L Jones
Expert
*****




Hit it

   
WWW

Gender: male
Posts: 978
Re: generate key press from midi events
« Reply #1 on: Dec 28th, 2003, 10:52am »
Quote Quote Modify Modify

Something like this? http://bome.com/midi/translator/
IP Logged

"...Playing fast around the drums is one thing. But to play with people for others, to listen to, that's something else. That's a whole other world." -- Tony Williams
kotto03
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #2 on: Dec 28th, 2003, 12:01pm »
Quote Quote Modify Modify

Oh it's great and shure helpfull.
But it cannot make a difference between e.g a controller value. If I decrease or increase the controller it is the same translation. Or I have to use two different controller e.g. controller #01 for inc and #02 for dec.
 
Thanks
 
kotto
IP Logged
Peter L Jones
Expert
*****




Hit it

   
WWW

Gender: male
Posts: 978
Re: generate key press from midi events
« Reply #3 on: Dec 28th, 2003, 1:50pm »
Quote Quote Modify Modify

In which case, maybe you can use MIDI OX to translate the incoming events into something Bome's translator can do something with?
IP Logged

"...Playing fast around the drums is one thing. But to play with people for others, to listen to, that's something else. That's a whole other world." -- Tony Williams
kotto03
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #4 on: Dec 28th, 2003, 2:11pm »
Quote Quote Modify Modify

Good idea, I try it.
 
 
kotto
IP Logged
kotto03
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #5 on: Dec 29th, 2003, 4:29am »
Quote Quote Modify Modify

on Dec 28th, 2003, 1:50pm, Peter L Jones wrote:
In which case, maybe you can use MIDI OX to translate the incoming events into something Bome's translator can do something with?

 
 
Yes it works fine. The only thing I wish is that bome's translator can send events to different applications, not only to the app that have the focus.
 
kotto
IP Logged
Peter L Jones
Expert
*****




Hit it

   
WWW

Gender: male
Posts: 978
Re: generate key press from midi events
« Reply #6 on: Dec 29th, 2003, 7:21pm »
Quote Quote Modify Modify

Now for that, you could maybe (and I know nothing about it, so that's a very nebulous maybe) use MIDI OX scripting to pull the right window to be the active one.
IP Logged

"...Playing fast around the drums is one thing. But to play with people for others, to listen to, that's something else. That's a whole other world." -- Tony Williams
dayton
Member
**



MIDI-OX Rules!

   


Posts: 15
Re: generate key press from midi events
« Reply #7 on: Dec 28th, 2004, 5:46am »
Quote Quote Modify Modify

This is a year late, but maybe it will help someone:
I've used Bome's MidiTranslator with considerable success, and now have gone on to using MidiOx with scripting in order to get more detailed reactions to live performances.  The COM port is an easy way to be able to manipulate the midi-stream.
In order to automate certain aspects of programs which are running while MidiOx is running, you may need to emulate a key being pressed.  This functions exactly as it does when you press a key physically on the keyboard: the program you want to manipulate has to be activated and the part of the screen you want to have effected also must be "in focus".  Certain programs (such as MidiOx) have what is called a COM port, which allow them to react in a preprogrammed manner to input.  Using the COM port from MidiOx, I can open the program, get information about it and the MIDI environment, recieve and fire notes, etc.  I cannot, however, change the color of the background, create and save translator settings, or other miscellaneous operations.  This would be possible in, for example, MSWord, because the programmers for Word have allowed a nearly complete access to all program functions from the COM port.  I could easily use a script to react to MIDI-notes and open a Word-file, write a letter to my mom or dad (depending on what melody I am playing) and save it.  Other programs do not have any COM port at all (actually... most applications...) and can only be manipulated by using the keyboard and the mouse.
 
With that said:  it is easy to pass keys or entire texts to open and focused applications:
 
'(this is VBScript)
 
set keyboard = WScript.CreateObject("WScript.Shell")
dim i
function sendkeys(key)
 keyboard.sendkeys key
end function
 
WScript.Sleep 5000
 
i="It works!"
 
sendkeys(i)
 
 
 
This will send "It works!" to any application which is open and focused at the time of execution.  (Note: to test this, you must have an application running and focused to recieve the text.  Also, look in the language specifications to know how to send backspaces, cntrl, etc.)  As for automatically opening and/or focusing an application, I didn't manage with programs which don't have a COM port.  The Shell can recognize and manipulate Word, Excel, Notepad, Calculator, etc., but not Reason, Cubase or whatever.  This MAY be possible, but would not help me because it would require that a specific part of the (for instance) Reason-window be in focus for the key to have the desired effect, and that would have to be a preprogrammed aspect of reason.  For this reason, I haven't looked any farther into the question.
 
If anyone reads this: post some things about what you actually do musically with these tools; I would be very interested to know.
 
Dayton
IP Logged
dayton
Member
**



MIDI-OX Rules!

   


Posts: 15
Re: generate key press from midi events
« Reply #8 on: Jan 12th, 2005, 6:01am »
Quote Quote Modify Modify

Now that I have actually needed it, I found out how to load programs and focus/unfocus them in order to use the SendKeys method I described before.  The only thing which made this step confusing is that the wsh.shell doesn't correctly recognize spaces in pathnames, so the programmer has to know how to use quotation marks correctly.  Here are the examples:
 
 
'these examples are in VBScript, and will run automatically if stored in a .vbs file which is then opened in winxp, 2000 or other windows-versions with windows script host
 
 
set shell = WScript.CreateObject("WScript.Shell")    'this gives a short name to "Wscript.Shell"
 
shell.run """C:\Archivos de programa\Propellerhead\Reason\reason.exe"   'this command will run any program which is given with the correct path name.  Unfortunately, the Shell interpreter cannot understand spaces in folder or file names, so the odd use of quotations is necessary.
 
shell.appactivate "reason"   'this will put the window-focus on any program or file or folder which starts with the string in the quotes.  It won't maximise the window however, but leave it like it was.
 
shell.run """C:\Archivos de programa\Propellerhead\Reason\test1.rns"  'this will do either: 1)open Reason (the host program for all files ending in .rns) and open test1.rns as the current open and selected song in Reason.  or: 2)if Reason is already running, this will set the focus on test1.rns (in case there are other songs currently open.)
 
 
shell.run """C:\Archivos de programa\Propellerhead\Reason\reason.exe""C:\Archivos de programa\Propellerhead\Reason\test1.rns"  'this is another way to open Reason with a specific song already opened
 
 
 
 
In Microsoft's documentation about the wshShell, you can find ways of minimizing and maximizing windows and a few more tricks, but these are the main ones.  I use it in conjunction with the SendKeys method in order to manipulate Reason and other programs from MIDI instruments.  For instance, I can toggle record and play by playing certain notes, erase loops I have played in, etc.  This comes in handy because I work with installations of MIDI-generating surfaces and objects, so I can make a sort of gigantic, room-filling MIDI-sequencer which records and plays loops and is fitted with a reset-button.
 
Word of warning:  SendKeys doesn't have ready-made reactions to NumPad keys.
IP Logged
may
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #9 on: Sep 9th, 2005, 4:39am »
Quote Quote Modify Modify

Again: Almost one year later !
 
May be I´m blind ? I understand in the meantime to run a script to have impact on other applictions for loading a presetfile.  But......
 
How does MIDIOX start the scriptfile after receiving a programchange messageHuh?
 
Help is very much appreciated!!!
 
Thanks Dayton for coming back and updating the thread after having new information.
 
Thanks MidiOX-Team for this great software!!
 
May
IP Logged
dayton
Member
**



MIDI-OX Rules!

   


Posts: 15
Re: generate key press from midi events
« Reply #10 on: Sep 14th, 2005, 5:25am »
Quote Quote Modify Modify

Hi again.  
Its more simple than you think, but looks complicated.  The way it works is this:  In order to use scripting, you need to either program a script or take one of the examples provided with MIDIOX and change it to reflect your needs.  If it is a VBS example, you can edit it in any text-editor (in my case edit-plus, although you can just as well use "Notepad" (but not "word" or "wordpad".)  and you must save it as a .vbs file (save as name.vbs)
 
Then, instead of starting MIDIOX, start the VBS file.  It will start MIDIOX for you.
 
Here is a complete program.  In the Sub Sink_MidiInput( ts, port, stat, dat1, dat2), you will find all of the information you seek.  This is just an example to show how I did it.  I added a few comments (sentences which occur after the "  '  "  symbol) to help.  This program will pass any and all midi messages, but if you press the lowest a of a piano (midi note 21), it will put the window focus on Reason and attempt to open a reason song file with one of the names I put in the list.  (This wont work for you because you won't have a song by that name and probably don't have Reason.  Just put in the information you need.)    Have fun!
 
 
 
 
'this is the program necessary for "Polyurethane Brain"
option explicit
 
dim mox
dim str, strWrk
dim n, ii, nInst, id
dim bGo
dim i
dim retrigger
dim index1
dim lasteventtime
dim offset
dim increment
dim intime
dim outtime
dim songarray,song,keyboard,songnumber
songarray=array("empty", "pendulums", "spittingimage", "rain02", "wiremouth", "flickpiano", "penguin","promenade", "fryingonions", "dodgependulum", "mightymouse")
 
' Create objects
Set mox = WScript.CreateObject("Midiox.MoxScript.1", "Sink_")  
set keyboard = WScript.CreateObject("WScript.Shell")
str = "MIDI-OX"
n = mox.InstanceNumber
If n > 0 Then
  str = str & ":" & CStr( n )
Else
  MsgBox "No Instances"
End If
 
 
 
   mox.DivertMidiInput = 1
 
 
'these functions are just to give names to the keys I want to press.  Alternatively, I could just use the function sendkeys() in order to type in the key I want pressed.
function Playtoggle()
 sendkeys(" ")
end function
function sendkeys(key)
 keyboard.sendkeys key
end function
function stops()
 sendkeys("{Enter}")
end function
 
 
 
 
mox.FireMidiInput = 1
index1 = 1
 
MsgBox "Press OK to end the program"
mox.FireMidiInput = 0
mox.DivertMidiInput = 0
 
' All done
   mox.ShutdownAtEnd = True
 
 
Set str    = nothing
Set strWrk = nothing
Set mox    = nothing
 
' Exit Point
'------------------------------------------
 
 
 
Sub Sink_MidiInput( ts, port, stat, dat1, dat2)
 
'the first if-then statement sets the song to be used.  It accepts the note 21 (lowest a) in channel 8
 if stat=&h97 then
   'basically, I have a list of songs that are in my concert.  I defined the list as an array earlier.  During the concert, I can change the song to use by simply pressing the lowest key on my keyboard.  This puts "Reason" into focus, then focuses the Reason song I need, then presses the space-bar to start playback.  Easy.  There is only one thing I cannot do, and that is use the number-pad; VBScript doesnt allow it.  I could do it using VB or cScript.
  song=songarray(dat1)
   'this is the part which focuses on the "reason" program
  keyboard.appactivate "reason"
  stops()
  WScript.Sleep 300
   'this is the part which either opens a reason file, or if opened, sets the focus on a particular reason file.
  keyboard.run"""C:\Documents and Settings\Dayton.MUSIC\Mis documentos\sx\pblf\" & song & ".rns"
   'these sleep commands are just to make sure that what happened before has time to finish.  For instance, if I load a Reason file and then push "play toggle", it wont play until it is loaded.
  WScript.Sleep 300
  stops()
  WScript.Sleep 300
  stops()
  WScript.Sleep 300
   'this isnt important, it just resets the "pendulums" song that I am using
  if song="pendulums" then
    mox.OutputMidiMsg -1, &hB1, 12, 0
    mox.OutputMidiMsg -1, &hB1, 13, 0
    mox.OutputMidiMsg -1, &hB1, 14, 0
 
  end if
  WScript.Sleep 400
   'this starts these songs playing once they have been opened or focused
  if song="pendulums" or song="spittingimage" or song="fryingonions" or song="dodgependulum" then
   playtoggle()
  end if
 end if
 
 if dat1<>21 then
  mox.OutputMidiMsg -1, stat, dat1, dat2
 end if
 
 
 
End Sub
 
'------------------------------------------
' connection point sink for SysEx
 
Sub Sink_SysExInput( strSysEx )
   mox.SendSysExString strSysEx  
End Sub
 
'------------------------------------------
 
Sub Sink_OnTerminateMidiInput()
   MsgBox "MIDI Input Termination Received From MIDI-OX"
   mox.FireMidiInput = 0
   mox.DivertMidiInput = 0
End Sub
 
 
 
IP Logged
may
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #11 on: Oct 3rd, 2005, 6:24am »
Quote Quote Modify Modify

Hi Dayton,
thanks for coming back to keep up information.
For me this is really a "full load" and it seems that i have
material to try and fail for the next weeks !!!
 
But......
In the meantime i have  a script which:
 
opens midiox  
opens "Chainer" which is the program i`d like to manipulate
loads a file in chainer by sending "sendkey" messages
 
Remember I "only" wanted to load a specific file when midiox receives a programchange message "1 or 2 or ......n"
 
Seems to me that I´m only one step away from what I`d like
to do and that is a IF - THEN "thing".
 
How do I script:
 
IF
MIDIOX RECEIVES A PROGRAMCHANGE NR  "n"  HuhHuh?
 
The THEN-Part is already in place as i mentioned (I hope)
 
Thanks for your patience teaching a greenhorn!
 
May  
 
 
 
 
 
IP Logged
Jamie OConnell
Administrator
*****






   
WWW Email

Gender: male
Posts: 2027
Re: generate key press from midi events
« Reply #12 on: Oct 4th, 2005, 2:35pm »
Quote Quote Modify Modify

This is largely from memory, but you just need to pick apart the MIDI message supplied to the Input Sink (assuming you're using that method?).  You can 'AND' (mask off) the Status with F0 hex to pull off the status bits.  To get the Channel you 'AND' the status with 0F hex (chan: 0 - 15).  Program change is status of Cn hex, when n is the channel number.
 

'------------------------------------------
' Example for Program Change (Status = Cn hex, n = channel)
'
Sub Sink_MidiInput( ts, port, stat, dat1, dat2)
 
   ' Check for program change
   If (stat And &hF0) = &hC0 Then
      If dat1 = 0  Then ' Prog Change range: 0 to 127
         . . .
      ElseIf dat1 = 1  Then ' Prog Change: 1
         . . .
      ElseIf dat1 = 2  Then ' Prog Change: 2
         . . .
      End If
   End If      
End Sub
 
« Last Edit: Oct 4th, 2005, 2:44pm by Jamie OConnell » IP Logged

--Jamie
Music is its own reward.

may
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #13 on: Oct 23rd, 2005, 7:06pm »
Quote Quote Modify Modify

Jamie,
thanks really for your answer!!!
 
Think you realized that I do not understand scripting but anyway tried to use some in the past in other projects and
these things are running because I can copy a code and at least understand the part I have to customize.  
 
The fact that over 900 people viewed this thread shows how important this thing seems to be and therefore here is my question!
 
Is it possible to helpout with the script for the folliwing:
 
IF MIDIOX receives  
MIDICHANNEL=6  
PROGRAMM CHANGE NR =1
run a file with the name "filename"
 
Please no comments, no further explainations,
no hex mid whatever language
 
ONLY THE CODE
 
Thanks in advance for teaching greenhorns!! Heard about big rewards in heaven for this hard work.
 
May
 
 
 
 
 
 
 
 
IP Logged
Jamie OConnell
Administrator
*****






   
WWW Email

Gender: male
Posts: 2027
Re: generate key press from midi events
« Reply #14 on: Oct 24th, 2005, 12:24am »
Quote Quote Modify Modify


Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
 
. . .
Sub Sink_MidiInput( ts, port, stat, dat1, dat2)  
   If stat = &hC5 AND dat1 = 0 Then  
      WshShell.Exec("filename")
   End If        
End Sub

 
I have to comment on 2 things: 1) I used 5 for MIDI channel because they are coded internally in MIDI 0 - 15.  2) I used 0 for Patch #1 because in MIDI they are coded 0 - 127. So 0 is the first patch number.
« Last Edit: Oct 24th, 2005, 2:19am by Jamie OConnell » IP Logged

--Jamie
Music is its own reward.

may
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #15 on: Nov 5th, 2005, 6:08pm »
Quote Quote Modify Modify

Hello Jamie,
 
not evrey day online but still working on the thing!
 
thanks really for your answer, but....... doesn`t work yet.
Please find attached script:
 
Set mox = WScript.CreateObject("Midiox.MoxScript.1", "Sink_")
 
Dim WshShell  
Set WshShell = CreateObject("WScript.Shell")  
 
Sub Sink_MidiInput( ts, port, stat, dat1, dat2)  
   If stat = &hC5 AND dat1 = 0 Then  
      WshShell.Exec("D:\Programme\chainer\chainer.exe")  
   End If        
End Sub  
Mox.ShutdownAtEnd = 0
 
This is what I use but don´t have any reaction on the following  
 
inputmonitor
Port 10 status C5     data1  00    data2  --    Chan  6
 
 
have the feeling that there is no midiinput available. May be
(hope) not a surprise for you.
Are there any other features in midiox to think about before
the script will workHuh Mapping, filter etcHuh
 
Your help is very much appreciated
 
Thanks
 
May
 
 
 
 
« Last Edit: Nov 5th, 2005, 6:12pm by may » IP Logged
Jamie OConnell
Administrator
*****






   
WWW Email

Gender: male
Posts: 2027
Re: generate key press from midi events
« Reply #16 on: Nov 6th, 2005, 2:16pm »
Quote Quote Modify Modify

In order for the event sink to start processing, you have to turn it on with mox.FireMidiInput = 1.  In addition, you have to do something to prevent the script from exiting -- usually something as simple as: MsgBox "Press OK to end MIDI Loop".  When you're done you have to turn off processing of MIDI: mox.FireMidiInput = 0.
 
So you need to add something like this to your script:

mox.FireMidiInput = 1
MsgBox "Press OK to end MIDI Loop"
mox.FireMidiInput = 0

 
Here's an entire script that works:
 

dim mox
Dim WshShell
 
' Create object
Set mox = WScript.CreateObject("Midiox.MoxScript.1", "Sink_")  
 
Set WshShell = CreateObject("WScript.Shell")  
 
mox.DivertMidiInput = 0    ' do NOT divert input for example
mox.FireMidiInput = 1  ' Start processing MIDI
MsgBox "Press OK to end MIDI Loop"  ' Will not return until after [OK]
mox.FireMidiInput = 0  ' All done processing
mox.DivertMidiInput = 0
 
mox.ShutdownAtEnd = False
 
Set mox        = nothing   ' Cleanup
Set WshShell = nothing
 
' Exit Point
'------------------------------------------
 
Sub Sink_MidiInput( ts, port, stat, dat1, dat2)
 
'  Launch program (Notepad)
    
   If stat = &hC5 AND dat1 = 0 Then    
         WshShell.Exec("notepad.exe")  
   End If    
      
End Sub
« Last Edit: Nov 6th, 2005, 2:21pm by Jamie OConnell » IP Logged

--Jamie
Music is its own reward.

may
New Member
*



MIDI-OX Rules!

   


Posts: 5
Re: generate key press from midi events
« Reply #17 on: Nov 12th, 2005, 11:06am »
Quote Quote Modify Modify

I T   W O R K S !!!!!
 
Jamie,
 
Thanks really for your time and your patience!!
For a very long time I was looking for these few lines.
I´m not through yet but gone a big part of the way.
 
Have to decide wether to store the different filenames to be loaded as VST patches to an Exel sheet or an Access-database.  
 
In the case of having problems, I hope you allow another question.
 
Thanks  
 
May
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »


MIDI-OX User Forum » Powered by YaBB 1 Gold - SP 1.3.1!
YaBB © 2000-2003. All Rights Reserved.