Mouse and Keyboard Control

So I’m in an introductory C++ course and I’m very new to OF. I’m trying to create a small simulation where I can control a circle on screen, moving it around with the arrow or WASD keys. However, I can’t for the life of me figure out how to implement this feature.

I’ve tried just putting the code directly into the void testApp::mousePressed(int x, int y, int button) function, but it doesn’t seem to do anything.

I’ve been searching online and I can’t find anything that clearly explains how this is supposed to work. How do I implement keyboard and mouse controls to move a shape on screen? I’d prefer to do this without classes, if possible.

Any help you could provide would be much appreciated. Thanks!

here a small example.

drag the mouse to move the circle or use the keys

  
  
#include "testApp.h"  
  
ofPoint myMouse;  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	  
	ofSetColor(255, 0, 0);  
	ofCircle(myMouse,40);  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
	int step = 10;  
	if(key == 'w') myMouse.y = myMouse.y - step;  
	if(key == 's') myMouse.y = myMouse.y + step;  
	if(key == 'a') myMouse.x = myMouse.x - step;  
	if(key == 'd') myMouse.x = myMouse.x + step;  
	  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
  
	myMouse.x = x;  
	myMouse.y = y;  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
  
}  
  
//--------------------------------------------------------------  
void testApp::gotMessage(ofMessage msg){  
  
}  
  
//--------------------------------------------------------------  
void testApp::dragEvent(ofDragInfo dragInfo){   
  
}  
  

Thank you so much! I knew it was going to be relatively simple. This really helps me out a lot, so I appreciate the help!

What we need to write in keyPressed for access to space, ctrl and alt ?