Is GetKeyboardState() function is toggle based? - c

I am just trying to get the all keyboard keys state. For that I wrote this simple program for trial.
I have made a separate thread for updating keystate.
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <pthread.h>
BYTE keys[256];
void *Thread(void *vargp);
void gotoxy(int x, int y);
COORD coord;
int main(){
pthread_t KEYSThread;
pthread_create(&KEYSThread,NULL,Thread,NULL);
while(1){
for(int i=0x41;i<=0x5A;i++){
printf("Key State %c: %d\n",i,keys[i]);
}
if(keys[0x31]){
if(keys[0x32]){
exit(0);
}
}
gotoxy(0,0);
}
}
void *Thread(void *vargp){
while(1){
GetKeyState(0);
//This is done to update the Keyboard buffer
//which in many cases windows won't do itself .. might be some bugs
GetKeyboardState(keys);
}
}
void gotoxy(int x, int y)
{
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Now when running initially it is all set to zero.
It shows the state of Keys A-Z keys.
As I press the keys the keys[] becomes 1 which is desirable but after I lift it should have gone to 0 but seems that is not the case.
Only after I press the keys again will the keys[i] go to 0.
It seems like GetKeyboardState() is identifying every keys as toggle buttons.
Can any one help me with this problem.
If you can suggest me some alternatives to GetKeyboardState(), please present a link to where I can learn about it (or syntax of the command).
OTHER THAN GetAsyncKeyState(),etc.
I want the state data to be transferred in a bulk rather than one by one.

Related

How do I can call initgraph more than one time?

Please look at the following code:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
void drawrect()
{
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
rectangle(500, 500, 700, 700);
getch();
cleardevice();
closegraph();
}
int main()
{
int f=1;
while(f)
{
char c;
printf("Press \"e\" to end, and any other character to draw a rectangle");
scanf("%c",&c);
c=='e' ? f=0:f=1;
drawrect();
fflush(stdin);
}
}
at the first time when I run this program, It works correctly and draws a rectangle, but after the first time, the rectangle function doesn't work and the GUI screen is completely blank, While I've cleared and closed previous graphic
So why it doesn't work at second time?
You code has undefined behaviour. The call to initgraph
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
should pass a pointer to the graphics mode you want to use. This page describes the function and its arguments, and about the mode it says:
*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals DETECT; in which case, *graphmode is set
by initgraph to the highest resolution available for the detected
driver). You can give *graphmode a value using a constant of the
graphics_modes enumeration type, which is defined in graphics.h and
listed below.
graphdriver and graphmode must be set to valid values from the
following tables, or you will get unpredictable results. The exception
is graphdriver = DETECT.
But you have not set the mode, and as the second paragraph quoted says, the result is unpredictable. This can be: working how you intended, not working, working strangely, or frying the processor.
So set the graphics mode you want to use with say
int gdriver = IBM8514, gmode = 0;
or whatever mode you need to use. Alternatively you can tell the system to detect for itself, in which case you can use
int gdriver = DETECT, gmode;
Init and close should be called just once and not be called in the drawrect but usually in the main instead ... also having getch in rendering routine makes no sense too...
I will not touch other issues here of your code as I am not coding console stuff for years and BGI even longer but I would start with reordering the code to this:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
void drawrect()
{
rectangle(500, 500, 700, 700);
}
int main()
{
int f=1;
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
while(f)
{
char c;
printf("Press \"e\" to end, and any other character to draw a rectangle");
scanf("%c",&c);
c=='e' ? f=0:f=1;
drawrect();
getch();
fflush(stdin);
}
cleardevice();
closegraph();
}
Also in future address the library by its real name BGI because graphics.h has no meaning as almost all gfx api/libs got a file with that name ...

How to use the move function under curses.h

It doesn't print at coordinates y=10, x=20.
#include <stdio.h>
#include <curses.h>
int main()
{
initscr();
refresh();
WINDOW *win;
wmove(win, 10, 20);
refresh();
printf("hi\n");
return 0;
}
When I execute it like this...
./a.out > op_file
This is what is op_file
[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi
Can someone explain...??
You must use initscr() function to initialize the screen and endwin() at the end to close the window...
If you move(), you must use refresh() or the cursor won't move physically.
To move the cursor to a new position on a window, use the function int wmove(WINDOW *win, int y, int x)
wmove(win, y, x);
where (x, y) are the coordinates of the new position in the window. If the window has nlines lines and ncolumns columns, then
0 <= y < nlines
0 <= x < ncolumns
Refresh. The actual cursor motion is not shown on the screen untill you do a wrefresh(win).
move(y, x) is equivalent to the wmove(stdscr, y, x).`
The move() and wmove() functions move the cursor associated with the current or specified window to (y, x) relative to the window's origin. This function does not move the terminal's cursor until the next refresh operation.
To move the logical cursor in the user-defined window my_window to the coordinates y = 5, x = 10, use :
#include <stdio.h>
#include <curses.h>
int main(){
refresh();//First refresh
WINDOW *my_window;
int a = wmove(my_window, 5, 10);
refresh();////Second refresh
printf("%d\n",a);
printf("hi\n");
return 0;
}
The output
[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi
shows the printable characters written. If you look at the complete text, e.g., in a text-editor, there'll be ASCII escape characters before the [ and ( characters since that's part of the escape sequence.
Your example doesn't show cursor movement (aside from the home position which you'd see as ^[[H near the end) because there's no reason for the curses library to actually move the cursor. If you had asked it to read a character, e.g., using getch, it would have to stop and decide where the cursor should be — and your wmove would do that — except that win is not initialized. The simplest thing to do is use stdscr (which is initialized by initscr).
The program quits curses calls without doing an endwin (which leaves the terminal in raw mode). The data does get written to the screen with the refresh call. The data written with printf happens to come out in the right order, but that's only coincidental since it does not use the same output buffering as ncurses.
Both of the other answers contain similar errors.
This works.
#include <stdio.h>
#include <curses.h>
int main()
{
initscr();
refresh();
WINDOW *win;
win = stdscr;
wmove(win, 10, 10);
refresh();
printf("hi\n");
return 0;
}
Thanks to #interjay.

DDA Line Drawing Algorithm has errors

Why am I getting an error saying 'setPixel not defined' with this code?
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<GL/glut.h>
inline int round(const float a)
{
return int (a+0.5);
}
void init(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
gluOrtho2D(0.0,200.0,0.0,200.0);
glMatrixMode(GL_PROJECTION);
}
void LineSegment(int xa, int ya,int xb,int yb)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
printf("Enter the initial value");
scanf("%d%d",&xa,&ya);
printf("Enter the final value");
scanf("%d%d",&xb,&yb);
int dx=xb-xa;
int dy=yb-ya;
int steps,k;
float xIncrement,yIncrement,x=xa,y=ya;
if(fabs(dx)>fabs(dy))
steps=fabs(dx);
else
steps=fabs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
setPixel(round(x),round(y));
for(k=0;k<steps;k++);
{
x += xIncrement;
y += yIncrement;
setPixel(round(x),round(y));
}
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutCreateWindow("DDA Line Algorithm");
glutDisplayFunc(LineSegment);
init();
glutMainLoop();
return 0;
}
Because there is no setPixel method in OpenGL or GLUT and as far as I can see from your code, you do not define one either. OpenGL deals with rendering primitives like points, lines, triangels etc, but not directly with setting single pixels on the screen. Since it is unclear what you want to achieve some suggestions:
If you want to draw a line in OpenGL, use the appropriate methods like glBegin(GL_LINES), etc. (although they are deprecated and should not be used anymore.) or glDrawArrays(GL_LINES, ....
If the goal is to implement a dda software rasterizer, then you might have to write the pixels to a texture and then display this texture.
Because you haven't defined setPixel anywhere. It's not an OpenGL call. You need to write it yourself, and it should set pixels on a buffer (if you're using double buffering) which you then later use as an argument to glDrawPixels(), or a call to the display buffer using glVertex2i(x,y). You can see an example of both approaches here and here.
Also, your LineSegment function is broken. In OpenGL you call glutDisplayFunc to specify a function which is called as fast as possible to render the display. However, in this function you call scanf() to prompt the user for data - this is broken. You should prompt them once at the start, and then pass that data into the function (which will then run as often as possible once glutMainLoop is called).

Stuck in programming with winBGIm with C

I recently discovered winBGIm libraries, and I find it very useful to learn to code, so I started creating something to get some practice but I got stuck with my program.
The program should show a little ball and two rectangles approaching to the ball itself, and the player can move the ball up and down simply by pressing a button on the keyboard. Initially, I wrote everything in the main to make it fast, but since this is terrible I divided the program into functions.
First of all, even when everything was together, the getch function seemed to not work, because, while it should wipe the input buffer for the kbhit function, it made the ball not move at all, while the mere kbhit function worked, but obvioulsy the ball continued going up even when you stopped pressing a key. I used the same procedure in another text-only program and it worked very well, so I don't know where the problem is.
The second and most important problem is that, after splitting the program into functions, it became static, since the main loop which would make the graphic move stops at the end of the first iteration. It only restarts working when I delete the cleardevice function at the end of the loop and I disable the duble buffering in the initwindow function, but I can't understand the relationship between these things.
Finally, when I set a new background color, if it is not 0 (black), the window remains completely black.
I hope someone can help me.
Best regards, Giacomo.
#include <graphics.h>
#include <time.h>
#include <stdio.h>
int rettangoli(int b);
void bird(int x_default, int y_default);
void bird();
int main() {
int a=640;
int b=480;
int x_default=150;
int y_default=400;
int verifica=0;
srand(time(NULL));
initwindow(a, b, "BGI", 0, 0, true, true);
setbkcolor(0);
while(1) {
bird(x_default, y_default);
verifica=rettangoli(b);
if(verifica==1) {
outtextxy(0, 0, "HAI PERSO");
outtextxy(0,20, "PREMERE UN TASTO PER CONTINUARE");
break;
}
delay(50);
while(kbhit()) {
getch();
}
swapbuffers();
cleardevice();
}
delay(350);
getch();
closegraph();
return 0;
}
void bird(int x_default, int y_default) {
static int x_pos=x_default;
static int y_pos=y_default;
if(kbhit()) {
if(y_pos-16>0)
y_pos=y_pos-5;
circle(x_pos, y_pos, 16);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x_pos, y_pos, 15);
}
else {
while(y_pos<400)
y_pos=y_pos+15;
circle(x_pos, y_pos, 16);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(x_pos, y_pos, 15);
}
return;
}
int rettangoli(int b) {
static int x_rett=615;
int verifica=0;
int contatore;
int altezza;
if(x_rett==615)
altezza=rand()%(b-60)+1;
rectangle(x_rett, 0, x_rett+25, altezza);
rectangle(x_rett, altezza+60, x_rett+25, 480);
setfillstyle(SOLID_FILL, GREEN);
floodfill(x_rett+1, 1, 15);
floodfill(x_rett+1, 479, 15);
for(contatore=0;contatore<altezza && x_rett>100;contatore++) {
if(getpixel(x_rett-1, contatore)!=getbkcolor())
verifica=1;
}
for(contatore=altezza+60;contatore<b && x_rett>100;contatore++) {
if(getpixel(x_rett-1, contatore)!=getbkcolor())
verifica=1;
}
for(contatore=0;contatore<15;contatore++) {
if(getpixel(contatore, altezza)!=getbkcolor())
verifica=1;
}
for(contatore=0;contatore<15;contatore++) {
if(getpixel(contatore, altezza+60)!=getbkcolor())
verifica=1;
}
x_rett=x_rett-5;
printf("%i", x_rett);
return verifica;
}
Nevermind, I solved the problem on my own. There was an incompatibility issue between Windows 8 x64 and Codeblocks-EP, and I think there is a general incompatibility between winBGIm and x64 systems.
I created a virtual machine using Vmware Player and Windows XP x86 and now everything is fine.

Simulate button click using GTK+ using gtk_event_put and a GdkEventButton structure

This is a follow-up to How to insert synthetic mouse events into X11 input queue
I'm trying to create a program that takes input from an external device and generates mouse clicks so that GTK+ will get and handle the events as if they mouse click happened normally.
Seems I can use a GdkEventButton structure:
https://developer.gnome.org/gdk/stable/gdk-Event-Structures.html#GdkEventButton
But I'm not sure how to determine the values to enter for each field. I'm looking for a small snippet of sample code or advice from somebody that has used gtk_event_put() with a GdkEventButton structure.
EDIT: If anybody knows a different or better way than my answer, please let me know.
There is another way, but this way I'm going to present, involve the use of pure X11, although seeing your question's tags, an answer that uses X11 is acceptable too.
That said, let me start by introducing the function where all happens, one thing, I'm going to be a bit verbose, in case of some other people that don't understand how use this function.
void
mouse_click(Display *display, int x, int y, int click_type, struct timeval *t);
display structure previously returned by XOpenDisplay() that contains all the information about the X server.
x the x coordinate of the pointer relative to the root of the screen.
y the y coordinate of the pointer relative to the root of the screen.
click_type the type of the click. For this answer, either MOUSE_RIGHT_CLICK or MOUSE_LEFT_CLICK
t timeval structure, specified the time interval the click should remain pressed.
Implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define portable_usleep(t) select(0, NULL, NULL,NULL, t)
enum { MOUSE_RIGHT_CLICK, MOUSE_LEFT_CLICK };
void
mouse_click(Display *display, int x, int y, int click_type, struct timeval *t)
{
Window root;
XEvent event;
root = DefaultRootWindow(display);
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
memset(&event, 0, sizeof(event));
event.xbutton.type = ButtonPress;
event.xbutton.button = click_type;
event.xbutton.same_screen = True;
XQueryPointer(display, root, &event.xbutton.root, &event.xbutton.window,
&event.xbutton.x_root, &event.xbutton.y_root,
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
while(event.xbutton.subwindow) {
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer(display, event.xbutton.window,&event.xbutton.root,
&event.xbutton.subwindow, &event.xbutton.x_root,
&event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y,
&event.xbutton.state);
}
if(XSendEvent(display, PointerWindow, True, 0xfff, &event)==0)
fprintf(stderr, "XSendEvent()\n");
XFlush(display);
portable_usleep(t); /* keeps the click pressed */
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, PointerWindow, True, 0xfff, &event)==0)
fprintf(stderr, "XSendEvent()\n");
XFlush(display);
}
Well, this function it's pretty straight forward, for example, left click at position 500,645 button pressed for half a second, this is how:
int
main(void)
{
int x;
int y;
Display *display;
struct timeval t;
display = XOpenDisplay(NULL);
if(!display) {
fprintf(stderr, "Can't open display!\n");
exit(EXIT_FAILURE);
}
x = 500;
y = 645;
t.tv_sec = 0;
t.tv_usec = 500000; /* 0.5 secs */
mouse_click(display, x, y, MOUSE_LEFT_CLICK, &t);
XCloseDisplay(display);
return 0;
}
Compile
$ gcc -o click click.c -lX11
After some research I was able to learn that GTK+/GDK 2.14 and newer have some functions for creating automated testing suites for GTK+ applications.
I was able to use gdk_test_simulate_button to simulate mouse clicks. This may not be the optimum solution, but right now it appears to work well.

Resources