I wonder if it is possible to create a program, using notepad and command prompt in windows, that acts like my fingers on the keyboard. I think the best way to explain what I mean is with an example.
for(i = 0; i < N; i++)
{
"press key x"
"wait 3 seconds"
}
So basically I want a program that acts as if I was hitting the x button every third second, a total of N times. If the answer is yes, it is possible, could you recommend a site, pdf or similar from where I could learn how to write such a program.
I hope that I have made myself clear, if there is anything in my question that should be improved, please let me know.
AutoHotKey is a nice option.
Here's a sample script:
Loop, 3
{
send, xxxxx
Sleep, 3000
}
For Windows, you can look into keybd_event and SendInput APIs for keyboard input, and Sleep for waiting.
For example, with keybd_event
for(i = 0; i < N; i++)
{
keybd_event((BYTE)VkKeyScan(L'x'), MapVirtualKey(VkKeyScan(L'x'), MAPVK_VK_TO_VSC), 0, 0);
keybd_event((BYTE)VkKeyScan(L'x'), MapVirtualKey(VkKeyScan(L'x'), MAPVK_VK_TO_VSC), KEYEVENTF_KEYUP, 0);
Sleep(3000);
}
With SendInput:
INPUT inputs[1];
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.wVk = VkKeyScan(L'x');
inputs[0].ki.wScan = MapVirtualKey(VkKeyScan(L'x'), MAPVK_VK_TO_VSC);
inputs[0].ki.time = 0;
inputs[0].ki.dwExtraInfo = 0;
for(i = 0; i < N; i++)
{
inputs[0].ki.dwFlags = 0;
SendInput(1, inputs, sizeof(INPUT));
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, inputs, sizeof(INPUT));
Sleep(3000);
}
Related
I want to write a function to let labels in the GUI fade in. I use a for loop. Each step in that loop i set the color values of that label higher. After each step i have a time delay of 200 milliseconds.
The following code works, except that the changes in label-color are only visible after the loop is done.
```
void fade(System::Windows::Forms::Label^ label) {
for (int i = 0; i < 10; i++) {
label->ForeColor = System::Drawing::Color::FromArgb(0, (i + 1) * 20, 0);
Sleep(200); // milliseconds
}
}
```
Is there a way to force the GUI to show changes immediately?
Or is there another way to let labels fade in?
label->Update(); did the trick. Thank you very much Hans.
void fade(System::Windows::Forms::Label^ label) {
for (int i = 0; i < 100; i++) {
label->ForeColor = System::Drawing::Color::FromArgb(0, (i + 1) * 2, 0);
Sleep(1); // milliseconds
label->Update();
}
}
I'm trying to make a program that uses my webcam in processing 3.x that will filter green and blue, from an image and show either red and black or white. I've tried two ways that I think should work, but I must be missing something here because my whole screen just goes black when I switch to this red mode.
Attempt 1:
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
set(x,y,color(red(x+(y*width)),0,0));
}
}
Attempt 2:
// inputImage is the image on my screen of type PImage.
for(int c = 0; c < inputImage.pixels.length; c++){
inputImage.pixels[c] = color(red(c),0,0);
}
Both attempts resulted in a black screen.
Attempt 2 is clearer way to do it, so I'll address that.
You need to call loadPixels() on the inputImage before you write to the pixels[] array and finally call updatePixels() to apply write changes.
You had also called red() on c (the loop index), not on img.pixels[c].
Result
inputImage.loadPixels();
for (int c = 0; c < inputImage.pixels.length; c++) {
img.pixels[c] = color(red(img.pixels[c]), 0, 0);
}
inputImage.updatePixels();
If you need the code to be faster consider setting the color without 2 method calls (to color() and red()) with this bit-mask:
img.pixels[c] = img.pixels[c] & 0xFF0000;
I am also working on the bootloader.
I had the problem in the following:
Once the cmd 'B' is received, later, 'F' is received, then I would start to call block load.
static void start_block_flash_load(uint16_t size, uint32_t *addr) {
uint16_t data_word;
uint8_t sreg = SREG;
uint16_t temp;
int i;
uint8_t my_size;
fprintf(lcdout, "B");
cli();
// Disable interrupts
(*addr) <<= 1;
if (size <= SPM_PAGESIZE) {
boot_page_erase(*addr);
boot_spm_busy_wait();
fprintf(lcdout, "%"PRIu16, size);
uint16_t i;
//store all values. PROBLEM here!!!
my_size = 208;
uint8_t buf[SPM_PAGESIZE] = { 0 };
for (i = 0; i < my_size; i++) {
//for (i=0; i<size; i++){
buf[i] = uart_getc();
// lcd_clear();
// lcd_setCursor(0, 2);
// fprintf(lcdout, "%3d", i);
// _delay_ms(500);
}
for (i = 0; i < my_size; i += 2) { //if size is odd, then use do-while
uint16_t w = buf[i];
w += buf[i + 1] << 8; //first one is low byte, second is high???
boot_page_fill((*addr)+i, w);
}
boot_page_write(*addr);
boot_spm_busy_wait();
(*addr) >>= 1;
uart_putc('\r');
} else
uart_putc('?');
boot_rww_enable ();
SREG = sreg;
}
I can see on the lcd that the size of the block is 256. However, when entering the loop to collect data, it will get stuck.
I tested with my_size and I found that only if my_size=208 the program will run further.
The strange thing is that if I put some statements inside the loop, e.g.
lcd_clear();
lcd_setCursor(0, 2);
then 'i' which I printed out on lcd will not go up to 140 something. I put different statements, the 'i' will give different value. That is very strange, since the uart_getc() will not lose data.
What I expect is that the loop will go up to 256. I cannot figure out what happened there.
Please help if you have any idea.
Thanks
break interrupts a for-loop in most languages, but in the case of nested loops I have never encountered an n-th order break.
1. Is there such a thing in any language?
2. If so what is the correct name for it?
3. If not, why?
NB. I am not looking for workarounds.
Regarding point 3. The closest thing I know is goto, which should not be used as it leads to spaghetti code (Python has it only in a joke module), but this seems like a different problem as a boolean variable to mark an inner break, catching a raised a custom error or moving the block to a function in order to break with return are a lot more convoluted (in terms of line numbers and variables in the code).
(This is a curiosity question from a theoretical point of view, but if it helps, I code primarily in Python, Matlab and JS. I have know Perl, Pascal and Basic, but I know only the basics of C++ and know shamefully little of machine code.)
Java has a labeled break statement that lets you break out of any number of loops:
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search; // <<=== This statement breaks both nested loops
}
}
}
I don't know of any language that lets you do this (apart from #dasblinkenlight example - and not saying there aren't any) but you can easily emulate it in any language that has the break statement.
I.e. conditionnaly break on a boolean exit loop var.
var exitLoops = false;
for (var i = 0; i < x; i++) {
for (var j = 0; j < y; j++) {
for (var k = 0; k < z; k++) {
if (something) {
exitLoops = true;
break;
}
}
if (exitLoops) break;
}
if (exitLoops) break;
}
No there isn't (as far as i know). And why ? because if you need to exit several nested for loops all at once, then you have a code design problem, not a syntax problem. all the answers given above, except of #PinkTurtle , uses some sort of goto statement , which is not recommended .
In JavaScript you can do this
Copy paste the following the Chrome Dev Console,
free:
for(var i=0; i<10; i++) {
for(var j=0; j<10; j++) {
for(var k=0;k<10;k++){
console.log('I am at i='+i+' , j='+j+ ' , k='+k);
if(k==3) {
console.log('I will now break FREE...');
break free;
}
}
}
}
console.log('... And Now I am Here...')
Output
I am at i=0 , j=0 , k=0
I am at i=0 , j=0 , k=1
I am at i=0 , j=0 , k=2
I am at i=0 , j=0 , k=3
I will now break FREE...
... And Now I am Here...
Need to animate a sorting algorithm, with source code line by line visualization.
INTRO:
For the begining, there is a FORM (see it in the picture attached). On top of that form is displayed a dinamicaly created array of Edit components, containing the array to sort.
A little below, on the right is placed a Memo component, containing the algorithm. At the left of each line of that algorithm, dinamicaly is placed a Label, that indicates the line number in algorithm.
The idea is to highlight line by colouring that label, where is the execution at the moment. Sorting starts when "Start" button is clicked. The action for it is following:
int n = 10;
bool swapped = true; hl(1);
int j = 0; hl(2);
int tmp; hl(3);
while (swapped) { hl(4);
swapped = false; hl(5);
j++; hl(6);
for (int i = 0; i < n - j; i++) { hl(7);
if (arr[i] > arr[i + 1]) { hl(8);
tmp = arr[i]; hl(9);
arr[i] = arr[i + 1]; hl(10);
arr[i + 1] = tmp; hl(11);
swapped = true; hl(12);
} hl(13);
} hl(14);
} hl(15);
The hl function must colour labels and pause execution by using Sleep() function
void TForm2::hl(int l)
{
for (int i = 0; i < 24; i++) {
Form2->lines[i]->Font->Color = clGray;
}
Form2->lines[l-1]->Font->Color = clRed;
Sleep(300);
}
PROBLEM:
Code execution is pausing (sleep function works properly), but the labels are still gray, with no visible changes, except the last one, when event finishes. The 15th line is red.
QUESTION:
Can anybody tell me, where I'm wrong, and how to do it right?
http://i.stack.imgur.com/crGyC.jpg
You need to allow the paint message to be processed in order to visually update the display. You can do that with either the Refresh or Update procedures:
Form2->Lines[l-1]->Font->Color = clGray;
Form2->Update(); // or Form2->Refresh();