Skip to content

Instantly share code, notes, and snippets.

@stungeye
Created February 2, 2023 21:30
Show Gist options
  • Save stungeye/23580e5fef648e2798c069d9e7f83816 to your computer and use it in GitHub Desktop.
Save stungeye/23580e5fef648e2798c069d9e7f83816 to your computer and use it in GitHub Desktop.
Detecting Keys and Keycodes in Openframeworks
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofAddListener(ofGetWindowPtr()->events().keyPressed, this,
&ofApp::keycodePressed);
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
}
void ofApp::keycodePressed(ofKeyEventArgs& e){
cout << "KEY : " << e.key << endl;
cout << "KEYCODE : " << e.keycode << endl;
cout << "MODIFIERS : " << e.modifiers << endl;
if (e.keycode == 83 ) {
if (e.modifiers == 0) {
cout << "Plain old s\n";
} else if (e.modifiers == OF_KEY_SHIFT) {
cout << "SHIFT-s\n";
} else if (e.modifiers == OF_KEY_ALT) {
cout << "ALT-s\n";
} else if (e.modifiers == OF_KEY_CONTROL) {
cout << "CTRL-s\n";
}
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void keycodePressed(ofKeyEventArgs& e);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment