Category: Tips

[Tips]How to get capture including mouse cursor

GOAL

To take screenshot with mouse cursor in Windows10 as below.

Environment

Windows10

Method1  Steps Recorder

1. Search the application “Steps Recorder” and start it.

2. Start Record

3. Move cursor to the position to capture and click

4. Stop Record

5. Save the result

Right-click on the result image, and click “Save picture as…”

Method2 Magnifier

1. Search the application “Magnifier” and start it.

2. Set the view “Docked” and resize the window

Resize the Magnifier window by dragging the corner of a window.

You can’t see the cursor in the magnifier window.

3. Press “PrintScreen” key or capture by Snipping Tool

Press “PtintScreen” key and capture the display.

Paste the copied image and trim the target area in the Magnifier window.

[Tips]How to detect “Enter Key” pressed in C/C++?

GOAL

To detect enter key pressed in C/C++ program.

Environment

Windows 10
Visual Studio 2017

Method

Method 1. get char

If you just detect enter key pressed at any time, use getchar() or cin.get() function.

C language

#include <stdio.h>
#include <windows.h> // for Sleep function

int main(void)
{
	while(true){
		if (getchar() == '\n') {
			printf("Enter key is pressed");
			Sleep(1000); //wait for check printed message.
			break;
		}
	}
	return 0;
}

C++

#include <iostream>
using namespace std;

int main() {
	while (1) {
		if (cin.get() == '\n') {
			cout << "Enter key is pressed" << endl;
			break;
		}
	}
	return 0;
}

Method 2. get line

If you’d like to detect that only enter key is pressed without input another key before enter, use scanf() , fgets() or gets() in C language or getline() in C++

C language

#include <stdio.h>
#include <string.h>
#include <windows.h> // for Sleep function

int main(void)
{
	while (1) {
		//if (getchar() == '\n') {
		char str[32];
		gets(str);
		if(strcmp(str,"")==0){
			printf("Enter key is pressed");
			Sleep(1000); //wait for check printed message.
			break;
		}
	}
	return 0;
}

C++

#include <iostream>
using namespace std;

int main() {
	string str;
	while (1) {
		getline(cin, str);
		if (str == "") {
			cout << "Enter key is pressed" << endl;
			break;
		}
	}
	return 0;
}

Method 3. GetKeyState

GetKeyState() is only provided on Windows. You can get the current “state” of the key. It returns 0 when the key is not pressed.

The key code is here.

#include <iostream>
#include <windows.h> // for GetKeyState function
using namespace std;

int main() {
	cout << "some process here" << endl;
	while (1) {
		if (GetKeyState(VK_RETURN) < 0) {
			cout << "Enter Key is Pressing" << endl;
		}
		else if (GetKeyState(VK_ESCAPE) < 0) {
			break;
		}
	}
	return 0;
}

What is pch.h

When I create a new C/C++ project in VisualStudio, an header file named “pch.h” is generated.

GOAL

To understand what is pch.h and how to use it.

Environment

Windows 10
VisualStudio 2017

What is pch.h

pch.h is a precompiled header that is an header file where any stable header files such as Standard Library headers are included. You can add header files you’d like to pre compile. The precompiled header is compiled only when it, or any files it includes, are modified.

Reference: Precompiled Header Files in Microsoft documentation

The following is initial pch.h

#ifndef PCH_H
#define PCH_H

// TODO: add headers that you want to pre-compile here
#endif //PCH_H

The advantage of pch.h

Using pch.h reduce compilation time, especially when applied to large header files, header files that include many other header files.

How to use pch.h

Add header files you’d like to use in the pch.h as below.

#ifndef PCH_H
#define PCH_H

#include <stdio.h>
#include <math.h>
#include "myheader.h"

#endif //PCH_H

How to start and kill application with shell script

GOAL

Start an application and kill it after a certain time.

Environment

macOS Catalina 10.15.5

ShellScript

sleep.sh

#!/bin/sh
open -a MyApp
sleep 60
killall MyApp
echo "stop"

open

The command “open” can open files and it takes some options. You can see options here. The -a option is for application.

open -a MyApp

kill/killall

The commands “kill” and “killall” can kill processes. “kill” takes PID(process id) and “killall” takes process name. You can stop and close an application with this command.

killall MyApp

sleep

The command “sleep” can delay for a specified time. Though “sleep” can take quantifiers such as 10s(second), 10m(minutes) and 10h(hours) in Linux, it can take number as seconds in OSX ([Tips]Why command “sleep 1m” doesn’t work in mac).

sleep 60

Execution

Open the terminal and input the sh file name.

 % Documents/sh/sleep.sh 

If the permission error occurred, change access permission with “chmod” command.

$ chmod +x sleep.sh 

[Tips]Why command “sleep 1m” doesn’t work in mac

Problem

In my shell script, “sleep 1m” command doesn’t wait 1 minutes but 1 second.

sleep.sh

#!/bin/sh
echo "start timer"
sleep 1m
echo "stop"

Environment

macOS Catalina 10.15.5

The cause of this problem

In Mac OS X, sleep command doesn’t work with quantifiers and takes argument num just as num seconds.

Reference: OSX bash “sleep”, Mac OS X Man Pages

Solution

Use 60 times minutes.

sleep.sh

#!/bin/sh
echo "start timer"
sleep 1*60
echo "stop"

[Tips] Where Is Python Module Directory?

GOAL

To find the directory where python libraries are.

Method

Use sys.path

You can get the list of module search paths. Find the directory you’d like from the list.

>python
Python 3.8.6
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
---The list of module search paths---

Appendix

You can find the directory where python.exe is by using “where” command.

>where python
C:\Users\<USER_NAME>\AppData\Local\Microsoft\WindowsApps\python.exe
C:\Python27\python.exe

How to get PID in Windows10

GOAL

Getting PID, Process ID.

Environment

WIndows10

What is PID?

PID, the process ID, is a number used in some operating system kernels to identify processes uniquely.

Methods

Method 1

Open “Task Manager” and open “Details” tab. See the PID of each Process.

Method 2

Use tasklist command and you can get the list of currently running tasks.

>tasklist

Image Name   PID   Session Name  Session# Mem Usage
============ ===== ============= ========= =========
System Idle  0     Services      0         8 K
System       4     Services      0         2,728 K
Registry     120   Services      0         36,960 K
...

Appendix

How to find the task by using its PID

Use tasklist command as below.

tasklist /svc /fi "PID eq <PID number>"

[Tips] How To Turn Features on/off in Windows10

GOAL

To turn Windows features on and off in Windows10.

Environment

WIndows10

Method

1. Open Control Panel

Press Windows key + R and open “Control Panel”.

Or click Windows System > Control Panel

2. Open “Turn Windows features on or off”

Click “Programs”.

Click “Turn Windows features on or off”.

Select the feature you’d like to turn on or off.

How to embed code in header of child-theme

I embed Google Analytics tag in my WordPress in “How to use Google Analytics in WordPress site“. And I created child-theme in “How To Create Child Themes In WordPress“. But, how can I embed google analytics tags into the header of the child-theme?

GOAL

To embed code in heder.php by adding the function to “functions.php” in the child theme.

Method

Use function add_action( ‘wp_head‘, <function_name> ), <<<(heredoc) and echo.

add_action( 'wp_head', 'header_customize');
function header_customize() {
	$header_tags = <<<EOM

	<!--ADD YOUR HTML HERE such as <script src="..."></script>-->

	EOM;
	echo $header_tags;
}

The argument function header_customize() is added when wp_head() is called in functions.php of parent theme as below.

<?php wp_head(); ?>

You can see the code written in the source of your website.

How To Use Google Fonts in your WordPress

GOAL

Change the default font of your theme in WordPress to google fonts.

Environment

WordPress 5.5.1
Theme: Sparkling (I have child-theme sparkling-child)

What is Google Fonts?

Google font is abundant font set that can be used in websites without install and upload. It can be used by just putting code in your HTML.

We believe the best way to bring personality and performance to websites and products is through great design and technology. Our goal is to make that process simple, by offering an intuitive and robust collection of open source designer web fonts.

Google Fonts

Method

1. Get the link to use Google Fonts

Choose the font you want to use from Google Fonts.

For example, I use “Open Sans” and “Roboto Slab” in this blog.

Copy the URL to link the fonts you selected.

You can see the css to use this fonts by accessing the URL you copied (example).

2. Enqueue the link with wp_enqueue_style()

The function wp_enqueue_style() can be used to add stylesheet in WordPress.

Add the function to functions.php in your child theme or parent theme.

add_action( 'wp_enqueue_scripts', 'add_fonts' );
function add_fonts() {
	wp_enqueue_style( 'font-styles' , 'https://fonts.googleapis.com/css2?family=Open+Sans:ital@0;1&family=Roboto+Slab:wght@600;700&display=swap' ,false);
}

In my case, I add the wp_enqueue_style() into the existing function in functions.php of child theme as below.

<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
	wp_enqueue_style( 'sparkling-bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );
	wp_enqueue_style( 'sparkling-icons', get_template_directory_uri() . '/assets/css/fontawesome-all.min.css', null, '5.1.1.', 'all' );
	wp_enqueue_style( 'font-styles' , 'https://fonts.googleapis.com/css2?family=Open+Sans:ital@0;1&family=Roboto+Slab:wght@600;700&display=swap' ,false);
    wp_enqueue_style( 'parent-style' , get_template_directory_uri() . '/style.css' );
}
?>

3. Change CSS

Change style.css to use added fonts. For example, I use “Roboto Slab” for heading elements.

h1, h2, h3, h4, h5, h6{
color: #DA4453;
font-weight: 700;
font-family: 'Roboto Slab', serif;
}