Programatically detect mouse or keyboard in C programming in VS code - c

I'm having trouble to make a programming that just simply detect mouse input programm in C programming. I tried to search some source codes, but everyone is using C++. Also even they use C language, they use <dos.h> and <graphics.h> which I cannot use. Also they are using Union REGS in and out, but I cant use thoese header file, so that I cant use them. Is there any other way I can make a programm that detect mouse input?? such as, if I just plug the mouse, then programm will just print mouse is detected, or mouse is connected well. Im using visual studio code. Thank you
#include <conio.h>
#include <direct.h>
#include <dos.h> // when I use this, complier said use direct.h
#include <graphics.h> // I've tried to add graphics.h files from winbGlm and paste it to Mingw lib but still not working.
#include <stdio.h>
union REGS in, out;
// Function to implement the functionality
// of detecting Mouse
void detectMouse()
{
in.x.ax = 0;
// Invoke interrupt (in86 method
// description mentioned above)
int86(0X33, &in, &out);
if (out.x.ax == 0)
printf("\nMouse Failed To Initialize");
else
printf("\nMouse was Successfully Initialized");
}

Related

How to get the id of the application receiving key strokes in C for macos?

I'm writing a program in C for OS X (for the terminal). As mentioned in the title, just need the id and/or name of the application receiving the keystrokes i.e. the focused window.
I've found the you can use frontmostApplication but I can't use it in C or can't figure out how to do it. I'm new to writing stuff in macOS, any help much appreciated.
The UI layer on macOS is all written in Objective-C, which you can't easily call from straight C code (well, technically, you could use objc_msgSend(), but trust me, you don't want to do that). Fortunately, the amount of Objective-C code you'll need to do this is quite small, and you can separate it out into a separate file which you include from your C code:
GetFrontApplication.h:
#ifndef GETFRONTAPPLICATION_H
#define GETFRONTAPPLICATION_H
char *GetFrontmostApplication(void);
#endif /* GETFRONTAPPLICATION_H */
GetFrontApplication.m:
#import Cocoa;
char *GetFrontmostApplication(void) {
#autoreleasepool {
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
return strdup(frontApp.localizedName.UTF8String);
}
}
Now you can just #include "GetFrontApplication.h" and call GetFrontmostApplication() from your C code, and you'll get a char * containing the name of the application. Make sure to free() the string after you're done with it.

Create simple display

Sorry for this simple question but I don't really understand how correctly create consol on C. I wanna have several dynamic lines that will be update. I know that I can use \r but it is only for one line. I want for several lines. system("cls") not working good for this. Maybe you can help me.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main() {
int currInt = 0;
while (true) {
system("cls");
printf("%d", currInt);
currInt++;
if (currInt == 5) {
currInt = 0;
}
}
}
I will be have asynchronous input data that will be display on several lines and I need have update this screen. I think about system("cls") but it not clear screen in loop. Endless loop is important.
I doubt it that printf() will help you achieve your goals. If I were you, I would give a shot with Ncurses. Check this question too: Where can I find a complete reference of the ncurses C API?
If this library won't satisfy you, then I would suggest curses.h. However I doubt this will do, since Ncurses is a modern implementation of the original curses.
For Windows: Is ncurses available for windows?

Compile simple C code in Visual Studio 2013

I am trying to compile a very simple C code in VS2013. I did a bit of Googling beforehand and I realised I need to follow certain steps for that e.g. change the compiler from Default to Compile As C Code in my project properties.
The problem is that when I compile following code:
#include <stdio.h>
main()
{
printf("Hello World! \n");
sleep(5);
}
I get these errors:
Error 1 error LNK2019: unresolved external symbol _sleep referenced in function _main
Error 2 error LNK1120: 1 unresolved externals
And if I change the code to:
#include <stdio.h>
#include <Windows.h>
main()
{
printf("Hello World! \n");
Sleep(5000);
}
Then it works fine.
How can I get my first code compiled successfully?
I have some tested codes in C and I need to use them. If I couldn't resolve above issue, then I have to update all syntaxes.
How can I get my first code compiled successfully?
You can't (at least exactly with this code as you stated). The point is that <stdio.h> header does not provide declaration for sleep() and C standard requires, that every function needs to be declared before its call. It's as simple as that.
The second thing is that sleep() function, that you want is likely from POSIX (specifically from <unistd.h> header). However, Visual Studio 2013 is not POSIX-compliant. Thus, the solution is to use Sleep(), that is declared in <Windows.h> header or possibly some other alternative.
Sleep is not a C standard function.
If you are on a UNIX system, then include <unistd.h>. However, if you are on a windows system, it should be in <windows.h> (which is why your second version of the code compiles successfully.
If you want to use in both systems, try something like this (from here):
#ifdef __linux__
//linux include code goes here
#elif _WIN32
// windows include code goes here
#else
#endif
The sleep function is not declared in <stdio.h>. If you're on unix, use sleep() from unistd.h, or for windows, Sleep() from Winbase.h (windows.h). If you want to see how Sleep() works, you can see how is sleep implemented at OS level?.
If you want to do it without having to change sleep() to Sleep(), you could use a macro:
#define sleep(s) Sleep((s) * 1000)
Macros are bad, so you may not want to do this in production code, but the advantage of the macro is that it will be guaranteed to be inlined, which will result in faster code.
sleep is a non-standard function:
On UNIX use <unistd.h>
On MS-Windows use <windows.h>
There is no way work around this besides writing your own code:
void sleep (int seconds)
{
Sleep (seconds * 1000); // Converting to milliseconds
}

TurboC++ graphics.h conflict with conio.h's clreol()

Note: This is TurboC++ so please don't expect STL
I have this simple code that have no other graphics.h functions rather than it's driver's declaration and call. I aim to:
Print a first string (A longer one)
Go to the first string's coordinates, clear that string (using clreol())
Print the second string which is shorter.
But I rather get this output on print of second string:
Shorter phrase.██████████████████████████████████████████████████████████████████
Here's my code:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int gdriver=DETECT, gmode;
void main(){
clrscr();
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
printf("Longer phrase than next.");
getch();
gotoxy(1,1);
clreol();
printf("Shorter phrase.");
getch();
}
When I remove the initgraph() function, it works fine, so there might be the problem, but of course I need it.
Haha - coding problems from stoneage ;). Thanks for this - it activated some nice memories.
My guess would be that you run into problems because you are mixing BGI (graphics) functions and "normal" text output. Try replacing the text output calls with calls to the corresponding BGI functions (if I remember correctly, this was called outtextxy() or something).

error: identifier "sytem" is undefined

I have just started to learn C language and I'm just trying to write Hello World to get started but I get this error message. I'm sure the answer is obvious but can someone please tell me what I need to do? This is my code:
#include <stdio.h>
int main()
{
printf("Hello World ");
system("Pause");
return 0;
}
#include<stdlib.h>
Include this header file..
You need to add another header file:
#include <stdlib.h>
When you have an undefined call like this you can always throw "man 3 system" and you'll get something like this so you can see if you're missing a header file.
FYI, for your specific program, you may want to consider no using system("Pause") since it's system dependent. It would be better to pause with a break point (if you're using an IDE) or something more C standard like getchar()
You need to #include <stdlib.h>
If you aren't sure which header a standard function is defined in, its man page will tell you.
Insert
#include <stdlib.h> //in C
or
#include <cstdlib> //in C++
before your main() function.
Note that your IDE should refrain from closing your program. If it doesn't, change IDE.
You should include the following library.
#include <stdlib.h>
It's simple as that.
I hope you find this useful.
As the others said, you need to include an header; if you're running on Linux, you may install "manpages-dev" package, and then tape "man system" which will tell you what are the headers you need to use.

Resources