Skip to content

Provide feedback when inserting notes using f1-f12 step input. #1253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions src/midiEditorCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,18 +1223,15 @@ void postMidiMovePitchCursor(int command) {
}
}

void cmdMidiInsertNote(Command* command) {
void cmdhInsertNote(int oldCount, int relativeNote, bool reportNewPos) {
HWND editor = MIDIEditor_GetActive();
MediaItem_Take* take = MIDIEditor_GetTake(editor);
int oldCount;
MIDI_CountEvts(take, &oldCount, nullptr, nullptr);
MIDIEditor_OnCommand(editor, command->gaccel.accel.cmd);
int newCount;
MIDI_CountEvts(take, &newCount, nullptr, nullptr);
if (newCount <= oldCount) {
return; // Not inserted.
}
int pitch = MIDIEditor_GetSetting_int(editor, "active_note_row");
int pitch = MIDIEditor_GetSetting_int(editor, "active_note_row") + relativeNote;
// Get selected notes.
vector<MidiNote> selectedNotes = getSelectedNotes(take);
// Find the just inserted note based on its pitch, as that makes it unique.
Expand All @@ -1250,9 +1247,6 @@ void cmdMidiInsertNote(Command* command) {
previewNotes(take, {note});
fakeFocus = FOCUS_NOTE;
ostringstream s;
// If we're advancing the cursor position, we should report the new position.
const bool reportNewPos = command->gaccel.accel.cmd ==
40051; // Edit: Insert note at edit cursor
if (settings::reportNotes) {
s << getMidiNoteName(take, note.pitch, note.channel) << " ";
s << formatNoteLength(note.start, note.end);
Expand All @@ -1266,6 +1260,18 @@ void cmdMidiInsertNote(Command* command) {
outputMessage(s);
}

void cmdMidiInsertNote(Command* command) {
HWND editor = MIDIEditor_GetActive();
MediaItem_Take* take = MIDIEditor_GetTake(editor);
int oldCount;
MIDI_CountEvts(take, &oldCount, nullptr, nullptr);
MIDIEditor_OnCommand(editor, command->gaccel.accel.cmd);
// If we're advancing the cursor position, we should report the new position.
const bool reportNewPos = command->gaccel.accel.cmd ==
40051; // Edit: Insert note at edit cursor
cmdhInsertNote(oldCount, 0, reportNewPos);
}

void cmdMidiPasteEvents(Command* command) {
HWND editor = MIDIEditor_GetActive();
MediaItem_Take* take = MIDIEditor_GetTake(editor);
Expand Down Expand Up @@ -2413,3 +2419,28 @@ void postMidiChangeZoom(int command) {
outputMessage(format(translate("{} pixels/second"), formatDouble(zoom, 1)));
}
}

// F1-f12 step input doesn't use actions, so we need to hook the key presses.
int midiStepTranslateAccel(MSG* msg, accelerator_register_t* accelReg) {
HWND editor = MIDIEditor_GetActive();
if (!editor || msg->message != WM_KEYDOWN || msg->wParam < VK_F1 ||
msg->wParam > VK_F12 ||
!GetToggleCommandState2(SectionFromUniqueID(MIDI_EDITOR_SECTION), 40053)) {
// This isn't for us.
return 0; // Normal handling.
}
MediaItem_Take* take = MIDIEditor_GetTake(editor);
int oldCount;
MIDI_CountEvts(take, &oldCount, nullptr, nullptr);
// F1 is the note at the pitch cursor, f2 is 1 semitone above, etc.
const int relativeNote = msg->wParam - VK_F1;
// If the shift key is being held, the cursor is not advancing, so we should
// not report the new position.
const bool reportNewPos = !(GetAsyncKeyState(VK_SHIFT) & 0x8000);
// We need to let the hook return so REAPER can handle the key and insert the
// note. We use CallLater to report the result.
CallLater([oldCount, relativeNote, reportNewPos] {
cmdhInsertNote(oldCount, relativeNote, reportNewPos);
}, 0);
return 0;
}
1 change: 1 addition & 0 deletions src/midiEditorCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ void postMidiToggleSnap(int command);
void postMidiChangeZoom(int command);
int countSelectedEvents(MediaItem_Take* take);
const std::string getMidiNoteName(MediaTrack* track, int pitch, int channel);
int midiStepTranslateAccel(MSG* msg, accelerator_register_t* accelReg);
10 changes: 8 additions & 2 deletions src/reaper_osara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6079,7 +6079,11 @@ HWINEVENTHOOK winEventHook = nullptr;
// This accelerator hook is registered at startup and remains registered until
// REAPER exits.
int translateAccel(MSG* msg, accelerator_register_t* accelReg) {
return vkbTranslateAccel(msg, accelReg);
int res = vkbTranslateAccel(msg, accelReg);
if (res != 0) {
return res;
}
return midiStepTranslateAccel(msg, accelReg);
}

extern "C" {
Expand Down Expand Up @@ -6156,7 +6160,9 @@ REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(REAPER_PLUGIN_HINSTANCE hI
accelReg.translateAccel = translateAccel;
accelReg.isLocal = true;
accelReg.user = nullptr;
plugin_register("accelerator", &accelReg);
// Using "<accelerator" causes this to be placed at the front of the
// accelerator list, giving it the first chance to sniff keystrokes.
plugin_register("<accelerator", &accelReg);
return 1;

} else {
Expand Down